detecting laser dots and get the coordinates

asked 2018-10-23 08:48:17 -0500

I am trying to get coordinates of laser dots using camera in iOS. For this I tried to detect all red covered in CV range then trying to get contours and moments. My code is given below :-

+ (nonnull UIImage *)cvtColorBGR2GRAY:(nonnull UIImage *)image {

cv::Mat bgr_image;

UIImageToMat(image, bgr_image);
cv::Mat blur_Image;

cv::medianBlur(bgr_image, blur_Image, 3);

// Convert input image to HSV
cv::Mat hsv_image;
cv::cvtColor(blur_Image, hsv_image, cv::COLOR_BGR2HSV);

// Threshold the HSV image, keep only the red pixels
cv::Mat lower_red_hue_range;
cv::Mat upper_red_hue_range;
cv::inRange(hsv_image, cv::Scalar(0, 100, 100), cv::Scalar(10, 255, 255), lower_red_hue_range);
cv::inRange(hsv_image, cv::Scalar(160, 100, 100), cv::Scalar(179, 255, 255), upper_red_hue_range);
cv::Mat allRedHue;
cv::addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 1.0, allRedHue);


std::vector<std::vector<Point2i> > contours;
std::vector<Vec4i> hierarchy;
findContours( allRedHue, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point2i(0, 0) );

/// Get the moments
std::vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
    mu[i] = moments( contours[i], false );
}

///  Get the mass centers:
std::vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
    mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
    printf("x %f",mu[i].m10/mu[i].m00);
    printf("y %f",mu[i].m01/mu[i].m00);

}

/// Draw contours
for( int i = 0; i< contours.size(); i++ )
{
    circle( allRedHue, mc[i], 10, Scalar(0,0,255), 1, CV_AA, 0 );
}


UIImage *grayImage = MatToUIImage(allRedHue);
return RestoreUIImageOrientation(grayImage, image);

}

But this function is not working. Any help will be highly appreciated.

edit retag flag offensive close merge delete

Comments

What do you mean by not working? Crashes, doesn't compile, does not output this what you expect? What does it output, maybe some photos to demonstrate the problem?

Jendker gravatar imageJendker ( 2018-10-23 11:07:22 -0500 )edit