OpenCV C++: How to find all the circles in an image - c++

Hi I am trying to find all the circles in the following image and Identify the defect.
this is my code:
static void findCircles2(const Mat& image)
{
vector<Vec3f> circles;
int thresh1 = 5;
Mat pyr, timg, gray0(image.size(), CV_8U), gray;
pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
for( int c = 0; c < 3; c++ )
{
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
Canny(gray0, gray, 0, thresh1, 5);
//dilate(gray, gray, Mat(), Point(-1,-1));
gray = gray0 >= (1)*255/N;
gray = gray0 >= (2)*255/N;
gray = gray0 >= (6)*255/N;
namedWindow( "Hough Circle Transform Demo 1", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo 1", gray );
waitKey(0);
HoughCircles( gray, circles, CV_HOUGH_GRADIENT, 1, gray.rows/8, 200, 100, 0, 0 );
cout<<"size of circles: "<<circles.size()<<endl;
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circle( gray, center, 3, Scalar(0,255,0), -1, 8, 0 );
circle( gray, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
/// Show your results
namedWindow( "Hough Circle Transform Demo 2", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo 2", gray );
waitKey(0);
}
}
Picture:
however the code is unable to find anything, I played arround with the thresholds but it doesnt help. please advise.
Development platform: VS2010, Opencv version: 2.4.10

Because the circles are so small and not that standard, so you cann't just do HoughCircles on the binary image.
An alternative method is to findContours, then filter the contours by ratio between the value of contourArea and the value of minEnclosingCircle.

Related

How to find a spot inside circle

Hi I am trying to process image1 so that I can find the spot as shown in image2
image1:
image2:
the code that I am using mergers the spot with the outer circle... is there away to go around this?
int thresh1 = 5;
Mat pyr, timg, gray0(image.size(), CV_8U), gray;
vector< Vec4i > hierarchy;
pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
Mat display_image = image.clone();
int ch[] = {0, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
Canny(gray0, gray, 0, thresh1, 5);
//dilate(gray, gray, Mat(), Point(-1,-1));
gray = gray0 >= (6)*255/11;
//gray = gray0 >= (1)*255/11;
//gray = gray0 >= (1)*255/11;
//gray = gray0 >= (1)*255/11;
imshow( "Hough Circle Transform Demo 2", gray );
waitKey(0);
Steps:
After find the circles, crop them.
Then resize using cv2.INTER_CUBIC(cv2.INTER_NEAREST is not good) .
Then threshold, and find contours on them again.
Use the area ratio (contour's area / minEnclosingCircle's area) to filter.
You can see the #12 is so different with others(ratio is small). So we get it #12.

OpenCV not detect a circle in the picture

I have a problem. I have the code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
/** #function main */
int main(int argc, char** argv)
{
Mat src, src_gray;
/// Read the image
src = imread( argv[1], 1 );
if( !src.data )
{ return -1; }
/// Convert it to gray
cvtColor( src, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
vector<Vec3f> circles;
/// Apply the Hough Transform to find the circles
HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.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][1]));
int radius = cvRound(circles[i][2]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
/// Show your results
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", src );
waitKey(0);
return 0;
}
This is an example from the website OpenCV ( http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html )
I have the following picture:
Image
For the image above code OpenCV does not detect the circle - circles.size() is equal 0.
What should I do? What to modify the code?
You're using the default thresholds (200 and 100) for an image with significant deviations from a circle (the solar flames). Not a programming error, just a data/settings issue.

How can I get the color histogram inside the circle in the image or the gradient distribution in the circle?

I have detected circular objects in the image with OpenCV and C++ and using houghcircle function. But I would like to filter out only those circle that have dark color (it should be the airplane door window). So I thought to use the color histogram inside the detected circle in the image or the gradient distribution in those circle to filter out .It works on some images like in the image provided. But when there are more circles in the image does not find the dark circle. Also I played with canny threshold but I would like it do happened automatically. So basically how to make the program more robust?
Firstly here is the code in C++ using OpenCV and C++
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
#include <vector>
int thresh = 200;
int max_thresh = 400;
Mat src;
void thresh_callback(int, void* );
int main()
{
cv::Mat src = cv::imread("d4.png");
resize(src, src, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* ) {
Mat src_gray;
cv::Mat bgr_image = cv::imread( "d4.png");
cv::Mat orig_image = bgr_image.clone();
cvtColor( bgr_image, src_gray, COLOR_BGR2HSV );
medianBlur(src_gray, src_gray, 3);
Mat canny_output;
Canny( src_gray, canny_output, thresh, thresh*3.5, 3 );
// Threshold the HSV image, keep only the black pixels
cv::Mat lower_black_hue_range;
cv::Mat upper_black_hue_range;
cv::inRange(canny_output, cv::Scalar(0, 0, 0), cv::Scalar(10, 10, 40), lower_black_hue_range);
cv::inRange(canny_output, cv::Scalar(0,0, 41), cv::Scalar(10, 15, 50), upper_black_hue_range);
// Combine the above two images
cv::Mat black_hue_image;
cv::addWeighted(lower_black_hue_range, 1.0, upper_black_hue_range, 1.0, 0.0, black_hue_image);
cv::GaussianBlur(black_hue_image, black_hue_image, cv::Size(9, 9), 2, 2);
// Use the Hough transform to detect circles in the combined threshold image
std::vector<cv::Vec3f> circles;
cv::HoughCircles(black_hue_image, circles, CV_HOUGH_GRADIENT, 1, black_hue_image.rows/1, 10, 100, 10, 0);
// Loop over all detected circles and outline them on the original image
if(circles.size() == 0) std::exit(-1);
for(size_t current_circle = 0; current_circle < circles.size(); ++current_circle) {
Point center(cvRound(circles[current_circle][0]), cvRound(circles[current_circle][1]));
int radius = cvRound(circles[current_circle][2]);
cv::circle(orig_image, center, radius, cv::Scalar(0, 255, 0), 5);
}
// Show images
resize(lower_black_hue_range, lower_black_hue_range, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window1 = "Threshold lower image";
namedWindow( source_window1, CV_WINDOW_AUTOSIZE );
imshow( source_window1, lower_black_hue_range );
//cv::namedWindow("Threshold lower image", cv::INTER_CUBIC);
//cv::imshow("Threshold lower image", lower_black_hue_range);
resize(upper_black_hue_range, upper_black_hue_range, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window2 = "Threshold upper image";
namedWindow( source_window2, CV_WINDOW_AUTOSIZE );
imshow( source_window2, lower_black_hue_range );
//cv::namedWindow("Threshold upper image", cv::INTER_CUBIC);
//cv::imshow("Threshold upper image", upper_black_hue_range);
resize(black_hue_image, black_hue_image, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window3 = "Combined threshold images";
namedWindow( source_window3, CV_WINDOW_AUTOSIZE );
imshow( source_window3, black_hue_image );
//cv::namedWindow("Combined threshold images", cv::INTER_CUBIC);
//cv::imshow("Combined threshold images", black_hue_image);
resize(orig_image, orig_image, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window4 = "Detected black circles on the input image";
namedWindow( source_window4, CV_WINDOW_AUTOSIZE );
imshow( source_window4, orig_image );
//cv::namedWindow("Detected black circles on the input image", cv::INTER_CUBIC);
//cv::imshow("Detected black circles on the input image", orig_image);
}
Input image of good detected circle
The output is correct
Second image with more circles
The output is wrong, wrong detected circle
Any help?

Hough algorithm don't find outer circle when there is a circle inside

I use hough transform to find circles in my picture, but it doesn't find outer circle.( I use opencv 2.4.2 and QT 3.3.0)
my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int HoughCircle1()
{
Mat src,dst, src_gray;
/// Read the image
src = imread("E:/imagesForImgProc/test.jpg",CV_LOAD_IMAGE_COLOR);
if( !src.data )
{
return -1;
}
/// Reduce the size of image
Size size(src.cols/2,src.rows/2);
resize(src, dst,size, 0, 0, CV_INTER_LINEAR);
/// Convert it to gray
cvtColor( dst, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
/// Apply the Hough Transform to find the circles
vector<Vec3f> circles;
HoughCircles( src_gray, circles,CV_HOUGH_GRADIENT, 1, src_gray.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][1]));
int radius = cvRound(circles[i][2]);
/// circle center
circle( dst, center, 3, Scalar(0,255,0), -1, 8, 0 );
/// circle outline
circle( dst, center, radius, Scalar(0,0,255), 1, 8, 0 );
}
/// Show your results
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", dst );
return 0;
}
int main()
{
HoughCircle1();
waitKey(0);
return 0;
}
"Orginal image"
"Code result "
"Wanted result"

remove bounding boxes inside bounding boxes for number recognition

I am trying a number recognition. However after contour finding. I get bounding boxes inside the main bounding box for numbers 0,6,8 ... as shown in figure. Please help me with this initial step of image processing.
I have tried using group rectangles but they are not working. Please check the code below. Thank you.
Image: http://tinypic.com/r/1twx05/5
int main()
{
Mat inimage, gray;
inimage = imread("sample.jpg");
cvtColor(inimage, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size(5,5), 0);
adaptiveThreshold(gray, gray, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 11, 0);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( gray, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
}
//groupRectangles(boundRect, 1, 0.2);
Scalar color = Scalar(0,0,255);
for( int i = 0; i< contours.size(); i++ )
{
//drawContours( inimage, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( inimage, boundRect[i].tl(), boundRect[i].br(), color, 1, 8, 0 );
}
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", inimage );
waitKey(0);
return 0;
}
try to use the flag: CV_RETR_EXTERNAL instead of CV_RETR_TREE
as stated in the docs it tells to take only outer contours.
Or follow the tree hierarchy to drop nested contours (read the docs for how-to)