Ask Your Question
0

Detecting color and drawing line

asked 2015-07-30 05:55:06 -0600

Ryang gravatar image

updated 2015-07-30 07:05:40 -0600

Hi guys. I'm just start OpenCV. I have some trouble to do my small project.

I want to make a program to detect orange color and draw line from middle to the orange color's average point . I have a trouble to draw line. anyone help me??

#include <opencv2/opencv.hpp> 
#include <iostream>  

int main( int argc, char** argv ) {    
cv::VideoCapture cap;   
if (argc==1) {    cap.open(0);           // open the default camera   } 
else {     cap.open(argv[1]);   }   
if( !cap.isOpened() ) 
    {  
    // check if we succeeded     
    std::cerr << "Couldn't open capture." << std::endl;     
    return -1;
    }
cv::Mat frame;  
while( 1 ) {    
    cap >> frame;   
    if( !frame.data ) break;             // Ran out of film 
    cv::Point point_of_poll = Find_poll (freame);
    if (point_of_poll.x == 0)
    draw_left;
    else if (point_of_poll < mid)
    draw_left;
    else if (point_of_poll > mid)
    draw_right;
    if( cv::waitKey(33) >= 0 ) break;   
} 
}


Point Find_poll(Mat frame){
    cv::CvSeq* lines = 0; 
    cv::Mat mask; // specify the range of colours that you want to include, you can play with the borders here
    cv::Scalar lowerb = cv::Scalar(38,125,250);
    cv::Scalar upperb = cv::Scalar(42,129,255); 
    cv::inRange(frame, lowerb, upperb, mask); // if the frame has any orange pixel, this will be painted in the mask as white
    cv::Mat storage;
    cv::Mat result;
    lines = cvHoughLines2( mask, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 5 );
    cv::Point Averge;
    cv::Point Averge.x = 0;
    cv::Point Averge.y = 0;
    for( i = 0; i < lines->total; i++ )
    {         
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
               Average.x += line.x;
        Average.y += line.y;
    }

Average.x = Point_Average.x / lines->total;
Average.y = Point_Average.y / lines->total;

return Average // return averge point of orange poll
}
edit retag flag offensive close merge delete

Comments

@thdrksdfthmn Just to understand... why you have used comment instead of answer ? I can see many of this case also from other user with high karma. Using comments leaves the question without answer and it encourages large discussions...it's just my point of view

pklab gravatar imagepklab ( 2015-07-31 03:28:26 -0600 )edit

Better now? :)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-07-31 04:43:00 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-07-30 08:01:53 -0600

thdrksdfthmn gravatar image

OK, I have a suggestion: change to HSV, so you can threshold the H channel for orange range and then apply moments to find out the center of the orange mass. Then center of the image you have it (x=rows/2, y=cols/2) and the center of mass you have detected it. All you need to do, now, is to call the function cv::line, with the 2 points (using cv::Point, instead of CvSeq and CvPoint pointers). Hough transform is used for detecting the lines, not for drawing them.

edit flag offensive delete link more

Comments

I don't know how to computer find the orange zone. So I think that after inRange there will left only white lines so if i use hough transform I can get the Point of the orange zone.

is there any better method?

Ryang gravatar imageRyang ( 2015-08-02 23:53:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-30 05:55:06 -0600

Seen: 291 times

Last updated: Jul 30 '15