Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp"

include <iostream>

include <termios.h>

using namespace std; using namespace cv;

int main(){ VideoCapture cap(0); if(!cap.isOpened()){ cout << "Error opening video stream or file" << endl; return -1;

}
while(1){ Mat frame; cap >> frame; if (frame.empty()) break; Mat hsv; cvtColor(frame, hsv, COLOR_BGR2HSV); Mat1b mask1, mask2; inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1); inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);

Mat mask = mask1 | mask2;

    Moments oMoments = moments(mask);

    double moment01 = oMoments.m01;
    double moment10 = oMoments.m10;
    double area = oMoments.m00;

    int posX = moment10/area;
    int posY = moment01/area;


    printf("position (%d,%d)\n", posX, posY);
    imshow("Mask", mask);
    char c=(char)waitKey(25);
if(c==27)
  break;

} cap.release(); destroyAllWindows();
return 0; }

click to hide/show revision 2
None

updated 2018-06-02 04:03:54 -0600

berak gravatar image

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp"

include <iostream>"opencv2/opencv.hpp"

include <termios.h>

#include <iostream>
#include <termios.h>
using namespace std;
using namespace cv;

cv; int main(){ VideoCapture cap(0); if(!cap.isOpened()){ cout << "Error opening video stream or file" << endl; return -1;

-1; }
while(1){ Mat frame; cap >> frame; if (frame.empty()) break; Mat hsv; cvtColor(frame, hsv, COLOR_BGR2HSV); Mat1b mask1, mask2; inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1); inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);

mask2);
Mat mask = mask1 | mask2;
 Moments oMoments = moments(mask);
 double moment01 = oMoments.m01;
 double moment10 = oMoments.m10;
 double area = oMoments.m00;
 int posX = moment10/area;
 int posY = moment01/area;
  printf("position (%d,%d)\n", posX, posY);
 imshow("Mask", mask);
 char c=(char)waitKey(25);
 if(c==27)
 break;
 }
cap.release();
destroyAllWindows();
return 0;
}

} cap.release(); destroyAllWindows();
return 0; }

click to hide/show revision 3
None

updated 2018-06-02 05:06:11 -0600

berak gravatar image

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

include "opencv2/opencv.hpp"

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp" #include <iostream> #include <termios.h> using namespace std; using namespace cv; cv;

int main(){
VideoCapture cap(0);
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
Mat hsv;
cvtColor(frame, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);
Mat mask = mask1 | mask2;
Moments oMoments = moments(mask);
double moment01 = oMoments.m01;
double moment10 = oMoments.m10;
double area = oMoments.m00;
int posX = moment10/area;
int posY = moment01/area;
printf("position (%d,%d)\n", posX, posY);
imshow("Mask", mask);
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}
click to hide/show revision 4
None

updated 2018-06-02 05:06:45 -0600

berak gravatar image

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp" #include <iostream> #include <termios.h> using namespace std; using namespace cv;

int main(){
  VideoCapture cap(0); 
  if(!cap.isOpened()){
    cout << "Error opening video stream or file" << endl;
    return -1;

  }  
  while(1){
    Mat frame;
    cap >> frame;
    if (frame.empty())
      break;
    Mat hsv;
    cvtColor(frame, hsv, COLOR_BGR2HSV);
    Mat1b mask1, mask2;
    inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1);
    inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);


    Mat mask = mask1 | mask2;

        Moments oMoments = moments(mask);

        double moment01 = oMoments.m01;
        double moment10 = oMoments.m10;
        double area = oMoments.m00;

        int posX = moment10/area;
        int posY = moment01/area;


        printf("position (%d,%d)\n", posX, posY);
        imshow("Mask", mask);
        char c=(char)waitKey(25);
    if(c==27)
      break;
  }
  cap.release();
  destroyAllWindows();   
  return 0;
}
click to hide/show revision 5
None

updated 2018-06-02 05:07:04 -0600

berak gravatar image

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.hue threshold

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp"
 #include <iostream>
 #include <termios.h>
 using namespace std;
 using namespace cv;

cv;
int main(){
VideoCapture cap(0);
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
Mat hsv;
cvtColor(frame, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);
Mat mask = mask1 | mask2;
Moments oMoments = moments(mask);
double moment01 = oMoments.m01;
double moment10 = oMoments.m10;
double area = oMoments.m00;
int posX = moment10/area;
int posY = moment01/area;
printf("position (%d,%d)\n", posX, posY);
imshow("Mask", mask);
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}

automatic hue threshold

I am new in opencv c++ and I wrote the code below to detect red color in real time video, but my teacher asked me to do automatic threshold with (hsv by using h) he did accept this code .please any help or advice will be appreciated.

#include "opencv2/opencv.hpp"
#include <iostream>
#include <termios.h>
using namespace std;
using namespace cv;


int main(){
  VideoCapture cap(0); 
  if(!cap.isOpened()){
    cout << "Error opening video stream or file" << endl;
    return -1;

  }  
  while(1){
    Mat frame;
    cap >> frame;
    if (frame.empty())
      break;
    Mat hsv;
    cvtColor(frame, hsv, COLOR_BGR2HSV);
    Mat1b mask1, mask2;
    inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1);
    inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);


    Mat mask = mask1 | mask2;

        Moments oMoments = moments(mask);

        double moment01 = oMoments.m01;
        double moment10 = oMoments.m10;
        double area = oMoments.m00;

        int posX = moment10/area;
        int posY = moment01/area;


        printf("position (%d,%d)\n", posX, posY);
        imshow("Mask", mask);
        char c=(char)waitKey(25);
    if(c==27)
      break;
  }
  cap.release();
  destroyAllWindows();   
  return 0;
}