Author Topic: Opencv Data Member Assignment and cout not working problem  (Read 755 times)

0 Members and 1 Guest are viewing this topic.

Offline galvatron

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Opencv Data Member Assignment and cout not working problem
« on: June 28, 2014, 08:52:04 am »

Hello there,


I'm new to Open CV (and EZ) and I was trying to download a source code from the blog for testing. http://opencv-srf.blogspot.in/2010/09/object-detection-using-color-seperation.html


Code: [Select]
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


using namespace cv;
using namespace std;


 int main( int argc, char** argv )
 {
//cout << "Start\n";
    VideoCapture cap(1); //capture the video from webcam


    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the web cam" << endl;
         return -71;
    }


    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"


  int iLowH = 170;
 int iHighH = 179;


  int iLowS = 150;
 int iHighS = 255;


  int iLowV = 60;
 int iHighV = 255;


  //Create trackbars in "Control" window
 createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
 createTrackbar("HighH", "Control", &iHighH, 179);


  createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
 createTrackbar("HighS", "Control", &iHighS, 255);


  createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
 createTrackbar("HighV", "Control", &iHighV, 255);


  int iLastX = -1;
 int iLastY = -1;


  //Capture a temporary image from the camera
 Mat imgTmp;
 cap.read(imgTmp);


  //Create a black image with the size as the camera output
 Mat imgLines = Mat::zeros( imgTmp.size(), CV_8UC3 );
 


    while (true)
    {
        Mat imgOriginal;


        bool bSuccess = cap.read(imgOriginal); // read a new frame from video




         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }


    Mat imgHSV;


   cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
 
  Mat imgThresholded;


   inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
     
  //morphological opening (removes small objects from the foreground)
  erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
  dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );


   //morphological closing (removes small holes from the foreground)
  dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
  erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );


   //Calculate the moments of the thresholded image
  Moments oMoments = moments(imgThresholded);


   double dM01 = oMoments.m01;
  double dM10 = oMoments.m10;
  double dArea = oMoments.m00;


cout << dM10 <<endl;
   // if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero
  if (dArea > 10000)
  {
   //calculate the position of the ball
   int posX = (int)dM10 / (int)dArea;
   int posY = (int)dM01 / (int)dArea;       
    //namedWindow("readings", CV_WINDOW_AUTOSIZE);   
   if (iLastX >= 0 && iLastY >= 0 && posX >= 0 && posY >= 0)
   {
    //Draw a red line from the previous point to the current point
    line(imgLines, Point(posX, posY), Point(iLastX, iLastY), Scalar(0,0,255), 2);

//cout << "Working" <<endl;
   }


    iLastX = posX;
   iLastY = posY;
  }


   imshow("Thresholded Image", imgThresholded); //show the thresholded image


   imgOriginal = imgOriginal + imgLines;
  imshow("Original", imgOriginal); //show the original image


        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break;
       }
    }


   return 0;
}


The problems that I face are:
1. When I try print any message after this line.


Code: [Select]
if (!bSuccess) //if not success, break loop
{
     cout << "Cannot read a frame from video stream" << endl;
     break;
}
cout << "Message" << endl;


The program does not print any message after this code


2. Another problem is in the following lines


Code: [Select]
//Calculate the moments of the thresholded image
Moments oMoments = moments(imgThresholded);


double dM01 = oMoments.m01;
double dM10 = oMoments.m10;
double dArea = oMoments.m00;


cout << dM10 <<endl;


The problem in the above code is that I can print dM10 variable, but I cant print dM01 and dArea variables. Also I can print oMoments.m10 but after assigning it to the variable dM10 I can't print the variable. I don't know where did I go wrong. Can you guys point me?


Thanks you.

LuckyA.Lot

  • Guest
Re: Opencv Data Member Assignment and cout not working problem
« Reply #1 on: June 28, 2014, 09:46:14 am »
Post an intro first. Intro is important. Your issues are not.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Opencv Data Member Assignment and cout not working problem
« Reply #2 on: June 28, 2014, 10:21:25 am »
Post an intro first. Intro is important. Your issues are not.
For when the post and replies might help others, even though the OP might never come back, type a reply if you have a constructive one.

I have never tried opencv though i have heard of it. Sari i ain't that useful.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

LuckyA.Lot

  • Guest
Re: Opencv Data Member Assignment and cout not working problem
« Reply #3 on: June 28, 2014, 11:04:18 am »
@kenjoe41
Besides the intro part, I'm not much help either. I know (some of) C++, but some of that code is beyond me.
@OP
Do you know much about that code, or are you merely copying and pasting it????

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Opencv Data Member Assignment and cout not working problem
« Reply #4 on: June 28, 2014, 03:58:43 pm »
I'm on my iPhone right now so I cat really do anything right now ill get on my computer soon though.
The most vulnerable part of an impenetrable system is those who believe it to be so.