OpenCV houghcircles deteting nothing C++ - c++

I am having a fairly annoying issue using OpenCV's HoughCircle method to detect circles in an image. I copied the code found in the official documentation and thus far I have been unable to detect anything. The circles vector has a size of 0 after calling the function, therefore there were no circles detected.
I have tried it with multiple images, such as .ppm images, .jpg and none of which end up with circles being detected. I really have no idea what could be going wrong.
If anyone has any idea of what I should try I'd be extremely thankful.
using namespace cv;
Mat src = imread("Images/balls.jpg");
if(! src.data )
{
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY );
GaussianBlur( src_gray2, src_gray2, cv::Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 1, src_gray2.rows/8, 200, 100, 0, 0 );
std::cout << circles.size();

This works for me. I adjusted the arguments to the HoughCircles function. See below. Also, I used this book to help me out: OpenCV 2
#include <cstdio>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
int main(int argc, char** argv) {
using namespace cv;
cv::Mat src=cv::imread("JGRiM.jpg");
if (!src.data) {
std::cout << "ERROR:\topening image" <<std::endl;
return -1;
}
cv::namedWindow("image",CV_WINDOW_AUTOSIZE);
cv::imshow("image",src);
Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY );
GaussianBlur( src_gray2, src_gray2, cv::Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
2, // accumulator resolution (size of the image / 2)
5, // minimum distance between two circles
100, // Canny high threshold
100, // minimum number of votes
0, 1000); // min and max radius
std::cout << circles.size() <<std::endl;
std::cout << "end of test" << std::endl;
std::vector<cv::Vec3f>::
const_iterator itc= circles.begin();
while (itc!=circles.end()) {
cv::circle(src_gray2,
cv::Point((*itc)[0], (*itc)[1]), // circle centre
(*itc)[2], // circle radius
cv::Scalar(255), // color
2); // thickness
++itc;
}
cv::namedWindow("image",CV_WINDOW_AUTOSIZE);
cv::imshow("image",src_gray2);
cv::waitKey(0);
return 0;
}

You need to change param2 to a lower value to find more circles. For example in the image posted above in the comments to the question with param2 = 20 I found a circle around the tennis ball.
HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 1, src_gray2.rows/8, 200, 20, 0, 0 );

Related

C++ OpenCV - Find biggest object in an webcam stream and sort it by size

My goal is to find the biggest contour of a captured webcam frame, then after it's found, find its size and determine either to be rejected or accepted.
Just to explain the objetive of this project, i am currently working for a Hygiene product's Manufacturer. There we have, in total, 6 workers that are responsible for sorting the defective soap bars out of the production line. So in order to gain this workforce for other activities, i am trying to write an algorithm to "replace" their eyes.
I've tried several methods along the way (findcontours, SimpleBlobDetection, Canny, Object tracking), but the problem that i've been facing is that i can't seem to find a way to effectively find the biggest object in a webcam image, find its size and then determine to either discard or accept it.
Below follows my newest code to find the biggest contour in an webcam stream:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
Mat src;
Mat imgGrayScale;
Mat imgCanny;
Mat imgBlurred;
/// Load source image
VideoCapture capWebcam(0);
if (capWebcam.isOpened() == false)
{
cout << "Não foi possível abrir webcam!" << endl;
return(0);
}
while (capWebcam.isOpened())
{
bool blnframe = capWebcam.read(src);
if (!blnframe || src.empty())
{
cout << "Erro! Frame não lido!\n";
break;
}
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;
Mat thr(src.rows, src.cols, CV_8UC1);
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
cvtColor(src, imgGrayScale, CV_BGR2GRAY); //Convert to gray
GaussianBlur(imgGrayScale, imgBlurred, Size(5, 5), 1.8);
Canny(imgBlurred, imgCanny, 45, 90); //Threshold the gray
vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours(imgCanny, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
for (int i = 0; i < contours.size(); i++) // iterate through each contour.
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a > largest_area)
{
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
Scalar color(255, 255, 255);
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
imshow("src", src);
imshow("largest Contour", dst);
waitKey(27);
}
return(0);
}
And here are the results windows that the program generates and the image of the object that i want to detect and sort.
Thank you all in advance for any clues on how to achieve my goal.

opencv detects multiple small circles, but not larger one

I am very new to OpenCV and I am trying to detect just the penny image, but I am getting a bunch of smaller circles. Can someone tell me what Im doing wrong?
Code from here: https://github.com/opencv/opencv/blob/master/samples/cpp/houghcircles.cpp
Only things I changed were to make min circle radius 400, and max of circle 0. Because I know the image will be 600x480 so the penny circle must be at least 400.
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
}
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv,
"{help h ||}{#image|../data/board.jpg|}"
);
if (parser.has("help"))
{
help();
return 0;
}
//![load]
string filename = parser.get<string>("#image");
Mat img = imread(filename, IMREAD_COLOR);
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
//![load]
//![convert_to_gray]
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
//![convert_to_gray]
//![reduce_noise]
medianBlur(gray, gray, 5);
//![reduce_noise]
//![houghcircles]
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows/16, // change this value to detect circles with different distances to each other
100, 30, 400,0 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
//![houghcircles]
//![draw]
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
circle( img, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
circle( img, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
}
//![draw]
//![display]
imshow("detected circles", img);
waitKey();
//![display]
return 0;
}
You've got radius and diameter mixed up. Your minimum radius cannot be 400 if your image is only 600x480. Set your min_radius to 200.
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
max(gray.cols,gray.rows), // to find only the biggest perfect circle
100, 100, 0,0 // leave other params as default
);

How to count white object on Binary Image?

I'm trying to count object from image. I use logs photo, and I use some steps to get a binary image.
This is my code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <features2d.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
//load image
Mat img = imread("kayu.jpg", CV_LOAD_IMAGE_COLOR);
if(img.empty())
return -1;
//namedWindow( "kayu", CV_WINDOW_AUTOSIZE );
imshow("kayu", img);
//convert to b/w
Mat bw;
cvtColor(img, bw, CV_BGR2GRAY);
imshow("bw1", bw);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY);
imshow("bw", bw);
//distance transform & normalisasi
Mat dist;
distanceTransform(bw, dist, CV_DIST_L2, 3);
normalize(dist, dist, 0, 2., NORM_MINMAX);
imshow("dist", dist);
//threshold to draw line
threshold(dist, dist, .5, 1., CV_THRESH_BINARY);
imshow("dist2", dist);
//dist = bw;
//dilasi
Mat dilation, erotion, element;
int dilation_type = MORPH_ELLIPSE;
int dilation_size = 17;
element = getStructuringElement(dilation_type, Size(2*dilation_size + 1, 2*dilation_size+1), Point(dilation_size, dilation_size ));
erode(dist, erotion, element);
int erotionCount = 0;
for(int i=0; i<erotionCount; i++){
erode(erotion, erotion, element);
}
imshow("erotion", erotion);
dilate(erotion, dilation, element);
imshow("dilation", dilation);
waitKey(0);
return 0;
}
As you can see, I use Erosion and Dilation to get better circular object of log. My problem is, I'm stuck at counting the object. I tried SimpleBlobDetector but I got nothing, because when I try to convert the result of "dilation" step to CV_8U, the white object disappear. I got error too when I use findContours(). It say something about channel of image. I can't show the error here, because that's too many step and I already delete it from my code.
Btw, at the end, i got 1 channel of image.
Can i just use it to counting, or am i have to convert it and what is the best method to do it?
Two simple steps:
Find contours for the binarized image.
Get the count of the contours.
Code:
int count_trees(const cv::Mat& bin_image){
cv::Mat img;
if(bin_image.channels()>1){
cv::cvtColor(bin_image,img,cv::COLOR_BGR2GRAY);
}
else{
img=bin_image.clone();;
}
if(img.type()!=CV_8UC1){
img*=255.f; //This could be stupid, but I do not have an environment to try it
img.convertTo(img,CV_8UC1);
}
std::vector<std::vector<cv::Point>> contours
std::vector<Vec4i> hierarchy;
cv::findContours( img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
return contours.size();
}
I have the same problem, here's an idea I'm about to implement.
1) Represent your image as an array of integers; 0 = black, 1 = white.
2) set N = 2;
3) Scan your image, pixel-by-pixel. Whenever you find a white pixel, activate a flood-fill algorithm, starting at the pixel just found; paint the region with the value of N++;
4) Iterate 3 until you reach the last pixel. (N-2) is the number of regions found.
This method depends on the shape of the objects; mine are more chaotic than yours (wish me luck..). I'll make use of a recursive flood-fill recipe found somewhere (maybe Rosetta Code).
This solution also makes it easy to compute the size of each region.
try to apply that on the your deleted img
// count
for (int i = 0; i< contours.size(); i = hierarchy[i][0]) // iteration sur chaque contour .
{
Rect r = boundingRect(contours[i]);
if (hierarchy[i][2]<0) {
rectangle(canny_output, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(20, 50, 255), 3, 8, 0);
count++;
}
}
cout << "Numeber of contour = " << count << endl;
imshow("src", src);
imshow("contour", dst);
waitKey(0);

Detecting Circle Using Hough Transform

I'm trying to detect circles with using hough transform.
With my current code I can detect the one below
But I want to find black hole inside the circle I've detected.
however changing parameters of houghcircle method is not helped me. Actually it found circles that are not exist.
Also I've tried crop the circle I've found and do another hough transform on this new part it also didn't help me.
here is my code
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp" // needs imgproc, imgcodecs & highgui
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src, circleroi;
/// Read the image
src = imread( "/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/delikli/gainfull.jpg", 2 );
/// Convert it to gray
// cvtColor( src, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src, src, Size(3, 3), 2, 2 );
// adaptiveThreshold(src,src,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,14);
vector<Vec3f> circles,circlessmall;
// Canny( src, src, 50 , 70, 3 );
/// Apply the Hough Transform to find the circles
HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][4]));
int radius = cvRound(circles[i][5]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
circleroi = src(Rect(center.x - radius, // ROI x-offset, left coordinate
center.y - radius, // ROI y-offset, top coordinate
2*radius, // ROI width
2*radius));
// imshow( "Hough Circle Transform Demo", circleroi );
}
resize(src, src, Size(src.cols/2, src.rows/2));
// threshold( circleroi, circleroi, 50, 255,CV_THRESH_BINARY );
// cout<<circleroi<<endl;
imshow("asd",src);
// imwrite("/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/cikti/deliksiz.jpg",circleroi);
waitKey(0);
return 0;
}
Update: since hough uses canny inside I'm manually used canny to see wether it finds the circle or not.
here canny results with
Canny(src,src, 100, 200,3);
thank you
You're setting one of the HoughCircles parameters minDist = src.rows/8, which is fairly large. The docs explain:
minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
The method can't return both the circle that it does find and the circle that you want, since they have nearly the same center (to within src.rows/8), just different sizes. If you set maxRadius to a value around 30 in order to exclude the larger circle, do you get the desired smaller circle?

What is causing the "Camera dropped frame!" error when running detectMultiScale using OpenCV?

I have a function that gets called from main in a for loop that searches for faces from a video feed. The code runs perfectly in the first run through, but on the second loop it outputs many "Camera dropped frame!" errors to the console and no longer updates the video feed.
I have found the line that causes the erros, it is the one that contains the detectMultiScale function in it. The full function is here:
void findInFrame(Mat inputFrame)
{
vector<Rect> faces;
Mat grayFrame;
cvtColor(inputFrame, grayFrame, COLOR_BGR2GRAY);
faceClassifier.detectMultiScale( grayFrame, faces);
for(int i=0;i<faces.size();i++)
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse(inputFrame,center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = grayFrame(faces[i]);
}
imshow("frame", inputFrame);
}
The line that throws the error is:
faceClassifier.detectMultiScale( grayFrame, faces);
Every frame after the first causes the errors. How can i fix this?
Main is here:
#include <iostream>
#include <unistd.h>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
string faceHaar = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
string eyesHaar = "/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml";
CascadeClassifier faceClassifier;
void findInFrame(Mat inputFrame);
int main(int argc, const char * argv[])
{
VideoCapture cam(0);
Mat frame;
if(!faceClassifier.load(faceHaar))
{
cout << "Error loading face cascade" << endl;
return -1;
}
for(;;)
{
cam >> frame;
if(!frame.empty())
{
findInFrame(frame);
usleep(1000);
}
else
{
cout << "frame empty" << endl;
}
}
return 0;
}
Try specify the function a bit more - I feel like its just taking too long to process your matches.
faceClassifier.detectMultiScale(grayFrame, faces, 1.3, 3,0|CV_HAAR_SCALE_IMAGE, Size(20, 30));
Where size is the size you trained your detector, 1.3 is a scale threshold and 3 is how many nearest neighbours are needed for a match.
Aside from that, dropping frames isn't a huge issue, but you could well be doing some things wrong elsewhere in your code, like where you grab your new frame.
I also would consider changing the function to void findInFrame(Mat &inputFrame) and calling imshow in your main loop, not in the function. Note that the &inputFrame isn't really a conventional pointer and doesn't require you to change how you reference inputFrame in the function