Search if a contour exists inside another contour - c++

I have two vector of contours OUTERCONT and INNERCONT defined in openCV as vector(vector(Points)). I want to check if one contour exists inside another.I would also like to know, how many contours exist inside each OUTERCONT.
I am currently drawing a minEnclosingRect around each contour and checking the following:
for (int i = 0; i < outerrect.size(); i++)
{
count = 0;
for (int j = 0; j < innerrect.size(); j++)
{
bool is_inside = ((innerrect[j] & outerrect[i]) == innerrect[j]);
if (is_inside == 1)
count++;
}
if (count > 0)
{
//DO SOMETHING
}
cout << count << endl;
This does not seem to be working, it always returns the count as some number around 120, which is not right. Could you suggest any change to make this work correctly?
NOTE: I cannot use hierarchy because these are two separate set of contours returned from 2 different functions.
I know PointPloygon test is an option, but could you suggest any more methods of doing this?

Here's my idea from the comments:
// stacked contours
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/Contours_in_Contours.png");
cv::Mat input_red = cv::imread("C:/StackOverflow/Input/Contours_in_Contours_RED.png");
cv::Mat reds;
cv::inRange(input_red, cv::Scalar(0, 0, 200), cv::Scalar(50, 50, 255), reds);
std::vector<std::vector<cv::Point> > contours1;
cv::findContours(reds, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::Mat input_yellow = cv::imread("C:/StackOverflow/Input/Contours_in_Contours_YELLOW.png");
cv::Mat yellows;
cv::inRange(input, cv::Scalar(0, 200, 200), cv::Scalar(0, 255, 255), yellows);
std::vector<std::vector<cv::Point> > contours2;
cv::findContours(yellows, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// now we have 2 sets of contours and want to find out whether contours of set2 are completely within a contour of contours1 without hierarchy information.
std::vector<cv::Mat> masks1;
std::vector<int> nMaskPixels1;
// for each contour in contours1: create a contour mask:
for (int i = 0; i < contours1.size(); ++i)
{
cv::Mat mask1 = cv::Mat::zeros(input.size(), CV_8UC1);
cv::drawContours(mask1, contours1, i, cv::Scalar::all(255), -1); // draw filled
int nPixel1 = cv::countNonZero(mask1);
masks1.push_back(mask1);
nMaskPixels1.push_back(nPixel1);
}
std::vector<cv::Mat> masks2;
std::vector<int> nMaskPixels2;
// for each contour in contours2: test whether it is completely within the reference contour:
for (int j = 0; j < contours2.size(); ++j)
{
cv::Mat mask2 = cv::Mat::zeros(input.size(), CV_8UC1);
cv::drawContours(mask2, contours2, j, cv::Scalar::all(255), -1); // draw filled
int nPixel2 = cv::countNonZero(mask2);
masks2.push_back(mask2);
nMaskPixels2.push_back(nPixel2);
}
for (int i = 0; i < masks1.size(); ++i)
{
cv::Mat mask1 = masks1[i];
// draw mask again for visualization:
cv::Mat outIm = input.clone();
cv::drawContours(outIm, contours1, i, cv::Scalar(0, 0, 0), 3);
for (int j = 0; j < masks2.size(); ++j)
{
cv::Mat mask2 = masks2[j];
cv::Mat overlap = mask1 & mask2;
int nOverlapPixels = cv::countNonZero(overlap);
if (nOverlapPixels == 0) continue; // no overlap at all. Test next contour.
if (nOverlapPixels == nMaskPixels2[j] && nOverlapPixels < nMaskPixels1[i])
{
// second contour is completely within first contour
cv::drawContours(outIm, contours2, j, cv::Scalar(0, 255, 0), 3);
}
else if (nOverlapPixels == nMaskPixels2[j] && nOverlapPixels == nMaskPixels1[i])
{
// both contours are identical
std::cout << "WARNING: " << "contours " << i << " and " << j << " are identical" << std::endl;
}
else if (nOverlapPixels < nMaskPixels2[j] && nOverlapPixels == nMaskPixels1[i])
{
// first contour is completely within second contour
std::cout << "WARNING: " << "contour " << i << " of the first set is inside of " << j << std::endl;
}
else if (nOverlapPixels < nMaskPixels2[j] && nOverlapPixels < nMaskPixels1[i])
{
// both contours intersect
cv::drawContours(outIm, contours2, j, cv::Scalar(255, 0, 255), 3);
}
}
cv::imshow("contours", outIm);
cv::imwrite("C:/StackOverflow/Output/contours.png", outIm);
cv::waitKey(0);
}
cv::imshow("input", input);
cv::waitKey(0);
return 0;
}
This code will create two sets of contours from these 2 images:
compute contour masks and compare them.
results will be displayed per contour. black contour is the reference, green are the ones that are completely within the reference, purple are intersecting contours.
I'm using this image to draw the results on:
getting these results:
contour1:
contour2:
contour3:
contour4:
contour5:
As you can see, the lonely yellow contour isn't detected to intersect or be contained in any of those red contours.

Related

Filtering For Only Red Contours Pixel By Pixel With an HSV Range

I'm trying to calculate the Mean & Std Deviation for red only contours. I suspect that HSV pixels for red Hue values of a Vec3b are stored from 0-10 and 165-179.
Here is my code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <vector>
#include <cmath>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
// Mat Declarations
// Mat img = imread("white.jpg");
// Mat src = imread("Rainbro.png");
Mat src = imread("multi.jpg");
// Mat src = imread("DarkRed.png");
Mat Hist;
Mat HSV;
Mat Edges;
Mat Grey;
vector<vector<Vec3b>> hueMEAN;
vector<vector<Point>> contours;
// Variables
int edgeThreshold = 1;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
int lowThreshold = 0;
// Windows
namedWindow("img", WINDOW_NORMAL);
namedWindow("HSV", WINDOW_AUTOSIZE);
namedWindow("Edges", WINDOW_AUTOSIZE);
namedWindow("contours", WINDOW_AUTOSIZE);
// Color Transforms
cvtColor(src, HSV, CV_BGR2HSV);
cvtColor(src, Grey, CV_BGR2GRAY);
// Perform Hist Equalization to help equalize Red hues so they stand out for
// better Edge Detection
equalizeHist(Grey, Grey);
// Image Transforms
blur(Grey, Edges, Size(3, 3));
Canny(Edges, Edges, max_lowThreshold, lowThreshold * ratio, kernel_size);
findContours(Edges, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
//Rainbro MAT
//Mat drawing = Mat::zeros(432, 700, CV_8UC1);
//Multi MAT
Mat drawing = Mat::zeros(630, 1200, CV_8UC1);
//Red variation Mat
//Mat drawing = Mat::zeros(600, 900, CV_8UC1);
vector <vector<Point>> ContourPoints;
/* This code for loops through all contours and assigns the value of the y coordinate as a parameter
for the row pointer in the HSV mat. The value vec3b pointer pointing to the pixel in the mat is accessed
and stored for any Hue value that is between 0-10 and 165-179 as Red only contours.*/
for (int i = 0; i < contours.size(); i++) {
vector<Vec3b> vf;
vector<Point> points;
bool isContourRed = false;
for (int j = 0; j < contours[i].size(); j++) {
//Row Y-Coordinate of Mat from Y-Coordinate of Contour
int MatRow = int(contours[i][j].y);
//Row X-Coordinate of Mat from X-Coordinate of Contour
int MatCol = int(contours[i][j].x);
Vec3b *HsvRow = HSV.ptr <Vec3b>(MatRow);
int h = int(HsvRow[int(MatCol)][0]);
int s = int(HsvRow[int(MatCol)][1]);
int v = int(HsvRow[int(MatCol)][2]);
cout << "Coordinate: ";
cout << contours[i][j].x;
cout << ",";
cout << contours[i][j].y << endl;
cout << "Hue: " << h << endl;
// Get contours that are only in the red spectrum Hue 0-10, 165-179
if ((h <= 10 || h >= 165 && h <= 180) && ((s > 0) && (v > 0))) {
cout << "Coordinate: ";
cout << contours[i][j].x;
cout << ",";
cout << contours[i][j].y << endl;
cout << "Hue: " << h << endl;
vf.push_back(Vec3b(h, s, v));
points.push_back(contours[i][j]);
isContourRed = true;
}
}
if (isContourRed == true) {
hueMEAN.push_back(vf);
ContourPoints.push_back(points);
}
}
drawContours(drawing, ContourPoints, -1, Scalar(255, 255, 255), 2, 8);
// Calculate Mean and STD for each Contour
cout << "contour Means & STD of Vec3b:" << endl;
for (int i = 0; i < hueMEAN.size(); i++) {
Scalar meanTemp = mean(hueMEAN.at(i));
Scalar sdTemp;
cout << i << ": " << endl;
cout << meanTemp << endl;
cout << " " << endl;
meanStdDev(hueMEAN.at(i), meanTemp, sdTemp);
cout << sdTemp << endl;
cout << " " << endl;
}
cout << "Actual Contours: " << contours.size() << endl;
cout << "# Contours: " << hueMEAN.size() << endl;
imshow("img", src);
imshow("HSV", HSV);
imshow("Edges", Edges);
imshow("contours", drawing);
waitKey(0);
return 0;
}
I've come across an issue in this particular case:
On the right is the original Image, The left displays the HSV mat, the Edge detection and an arrow is pointing to a contours Mat that I drew after the filtering.
Here is the source image:
After the filtering is complete I just calculate the Mean and STD.
I have a feeling that my range is incorrect for 0-10 and 165-179. Any suggestions or further improvements would help a lot.
Thanks.
A quick test shows me that the range is correct. Without all the contour extraction stuff, if I just filter the colors using 0-10 and 165-179 ranges, I get the two red boxes in the lower-middle range of your input image.
The contour artifact that you see might actually be coming from both a JPEG artifact (if you zoom in at the limit between the white and red box, you can see that it is gradual and not sharp, due to JPEG compression), and the fact that you are only thresholding in the Hue channel. At low saturation, many grey-ish colors which you don't want will actually be fitting within your hue threshold. The solution for that is to filter pixel values in the S and V channel as well.
In your code, that means changing the line if ((h <= 10 || h >= 165 && h <= 180) && ((s > 0) && (v > 0))) { to if ((h <= 10 || h >= 165 && h <= 180) && ((s > 50) && (v > 50))) {
The value 50 is working on that specific sample image, but of course the correct value will depend on your input image.

Wrong mass center point (opencv and moment function)

I'm trying to calculate the mass center of images using OpenCV and I got errors, as you can see in the images (the mass center must not be to closest of any side in this cases). Also, I got mass centers that depends of the rotation and that's incorrect.
Next, you can see the code, input image and output image.
I tried with different example codes, and the results are the same.
Output image: Mass center calculated by the program
Input image: Image Input
Example code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat srcGray;
RNG rng(12345);
int main(int argc, char **argv)
{
// Load source image and convert it to gray
src = imread(argv[1], 1);
// Convert image to gray and blur it
cvtColor(src, srcGray, CV_BGR2GRAY);
blur(srcGray, srcGray, Size(3, 3));
Mat srcThresh;
double otsu;
otsu = threshold(srcGray, srcThresh, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Mat cannyOut;
Canny(srcGray, cannyOut, otsu, otsu * 1 / 2, 3, 1);
// Find contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(cannyOut, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// Get the moments
vector<Moments> mu(contours.size());
for (int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
// Get the mass centers:
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);
}
// Draw contours
Mat drawing = Mat::zeros(cannyOut.size(), CV_8UC3);
string sObjectNumber; // string which will contain the result
ostringstream sContourNumber; // stream used for the conversion
for (int i = 0; i< contours.size(); i++)
{
// drawing.setTo(Scalar(0.0,0.0,0.0));
sContourNumber << i;
sObjectNumber = sContourNumber.str(); // Convert int to string
Point pCoordinates(mc[i].x + 3, mc[i].y - 3); // Text's coordinates (A little bit off from mass center)
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
circle(drawing, mc[i], 4, color, -1, 8, 0); // Draw mass center
putText(drawing, sObjectNumber, pCoordinates, CV_FONT_HERSHEY_COMPLEX, 1, color, 2, 8); // Write object number
sContourNumber.str(""); // Clear string
sContourNumber.clear(); // Clear any error flags
// imshow("Contours", drawing);
// waitKey();
}
double hu[7];
for (int i = 0; i < contours.size(); i++)
{
cout << "Contour: " << i << " Area: " << contourArea(contours[i]) << " Length: " << arcLength(contours[i], true) << "\n";
for (int j = 0; j < 7; j++)
{
HuMoments(mu[i], hu);
cout << "Contour: " << i << " Hu: " << j << " Result: " << hu[j] << "\n";
}
cout << "\n";
}
imshow("Contours", drawing);
waitKey(0);
return(0);
}
Very thanks for all!
Diego

counting the number of child contours inside a contour in opencv

I have this source image
and I have applied binary thresholding to get this
I used contours to differentiate between the ones having child contours and ones that don't.The resultant pic is
But how do I count the number of child contours that each green contour contains?. This is code I have used:-
Mat binMask = lung;// the thresholded image
Mat lung_src = imread("source.tiff");// the source image
//imshow("bin mask", binMask);
vector<std::vector<cv::Point>> contours;
vector<cv::Vec4i> hierarchy;
int count = 0, j;
double largest_area = 0;
int largest_contour_index = 0;
findContours(binMask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i;
}
for (j = 0; j <= i; j++)
{
if (hierarchy[j][2] != -1) // means it has child contour
{
drawContours(lung_src, contours, j, Scalar(0, 255, 0), 1, 8, hierarchy, 0, Point());
}
else // means it doesn't have any child contour
{
drawContours(lung_src, contours, j, Scalar(0, 0, 255), 1, 8, hierarchy, 0, Point());
}
}
}
drawContours(lung_src, contours, largest_contour_index, Scalar(255, 0, 0), 1, 8, hierarchy, 0, Point());
imshow("lung-mapped", lung_src);
EDIT-1- I added the code from Humam at the end to check it out:
std::vector<int> number_of_inner_contours(contours.size(), -1);
int number_of_childs = 0;
for (size_t i = 0; i < contours.size(); i++)
{
int first_child_index = hierarchy[i][2];
if (first_child_index >= 0)
{
int next_child_index = hierarchy[first_child_index][0];
if (number_of_inner_contours[next_child_index]<0)
{
number_of_childs = number_of_inner_contours[next_child_index];
}
else
{
while (next_child_index >= 0)
{
next_child_index = hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours[i] = number_of_childs;
}
}
else
{
number_of_inner_contours[i] = 0;
}
cout << "\nThe contour[" << i << "] has " << number_of_inner_contours[i] << "child contours";
}
But the output I got was like :
The contour[456 ] has 0 child contours
The contour[457 ] has 0 child contours
The contour[458 ] has 0 child contours
The contour[459 ] has -1 child contours
From OpenCV documentation :
hierarchy – Optional output vector, containing information about the
image topology. It has as many elements as the number of contours. For
each i-th contour contours[i] , the elements hierarchy[i][0] ,
hiearchyi , hiearchy[i][2] , and hiearchy[i][3] are set to
0-based indices in contours of the next and previous contours at the
same hierarchical level, the first child contour and the parent
contour, respectively. If for the contour i there are no next,
previous, parent, or nested contours, the corresponding elements of
hierarchy[i] will be negative.
This is untested code for doing the job:
std::vector<size_t> number_of_inner_contours;
number_of_inner_contours.reserve(contours.size());
for (size_t i = 0; i < contours.size(); i++){
size_t number_of_childs = 0;
auto first_child_index=hierarchy[i][2];
if(first_child_index>=0){
auto next_child_index=hierarchy[first_child_index][0];
while (next_child_index>=0){
next_child_index=hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours.emplace_back(number_of_childs);
}
else{
number_of_inner_contours.emplace_back(0);
}
}
This code could be done in a better way by using the concept of dynamic programming. This is a first try also:
std::vector<int> number_of_inner_contours(contours.size(),-1);
for (size_t i = 0; i < contours.size(); i++){
auto number_of_childs = 0;
auto first_child_index=hierarchy[i][2];
if(first_child_index>=0){
auto next_child_index=hierarchy[first_child_index][0];
if(number_of_inner_contours[next_child_index]<0){
number_of_childs=number_of_inner_contours[next_child_index];
}
else{
while (next_child_index>=0){
next_child_index=hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours[i]=number_of_childs;
}
}
else{
number_of_inner_contours[i]=0;
}
}

How can I found the circles on image avoiding the internal circles?

How can I found the circles on image using the method minEnclosingCircle but avoid the internal circles?
I'm using the opencv with c++ to detect circles on a image. I was using the method HoughCircles but sometimes it lost some circles or it detected false circles.
Therefore I'm replacing it for the minEnclosingCircle. Now the algorithm is finding all circles, but in some cases it found circles inside other circles and I want avoid this.
The image 1 is a example of input and the image 2 is it output.
The code used to process those images is this:
Start of code:
vector < Circle > houghCircles(Mat thresholdImage, float minRadius, float maxRadiuss) {
vector < vector < Point > > contours;
vector < Vec4i > hierarchy;
findContours(thresholdImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector < vector < Point > > contours_poly(contours.size());
vector < Circle > circlesTMP(contours.size());
for (int i = 0; i < contours.size(); i++) {
Point2f detectedCenter;
float detectedRadius;
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
minEnclosingCircle((Mat) contours_poly[i], detectedCenter, detectedRadius);
if (minRadius != 0 && detectedRadius < minRadius) {
continue;
}
if (maxRadiuss != 0 && detectedRadius > maxRadiuss) {
continue;
}
sf::Circle _circle(detectedCenter, detectedRadius);
circlesTMP.push_back(_circle);
}
vector < Circle > circles;
for (int i = 0; i < circlesTMP.size(); i++) {
sf::Circle _circle = circlesTMP[i];
if (_circle.getRadius() > 0) {
circles.push_back(circlesTMP[i]);
}
}
cout << "Circles found: " << circles.size() << endl;
Mat drawing = Mat::zeros(thresholdImage.size(), CV_8UC3);
for (int i = 0; i < circles.size(); i++) {
sf::Circle _circle = circles[i];
Scalar color = Scalar(200, 187, 255);
circle(drawing, _circle.getCenter(), (int) _circle.getRadius(), color, 2, 8, 0);
}
imshow(drawing, "OpenCVUtil");
waitkey();
return circles;
}
you can check this link out and change your mode parameter and test your code:
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
check out the results you obtain for CV_RETR_EXTERNAL instead of CV_RETR_TREE as the mode.

How to filter small segments from image in OpenCV?

I am printing the Contours in the following way:
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours( mask, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
for ( size_t i=0; i<contours.size(); ++i )
{
cv::drawContours( img, contours, i, Scalar(200,0,0), 1, 8, hierarchy, 0, Point() );
cv::Rect brect = cv::boundingRect(contours[i]);
cv::rectangle(img, brect, Scalar(255,0,0));
}
Once I have a binnary image, I want to eliminate the smaller contours. Any suggestions on how to do so?
MY INPUT PICTURE:
WHAT I WANT TO ACHIEVE:
EDIT:
I am trying to get rid of the smaller segments. Any hints?
I've built a function that only prints points that belong to the bigger segments. It uses the pointPolygonTest, an awesome OpenCV function that can say if a point is inside, outside or at the bounds of a given contour. Check it out:
// Gets only the biggest segments
Mat Morphology::threshSegments(Mat &src, double threshSize) {
// FindContours:
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat srcBuffer, output;
src.copyTo(srcBuffer);
findContours(srcBuffer, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
vector<vector<Point> > allSegments;
// For each segment:
for (size_t i = 0; i < contours.size(); ++i) {
cv::drawContours(srcBuffer, contours, i, Scalar(200, 0, 0), 1, 8, hierarchy, 0, Point());
cv::Rect brect = cv::boundingRect(contours[i]);
cv::rectangle(srcBuffer, brect, Scalar(255, 0, 0));
int result;
vector<Point> segment;
for (unsigned int row = brect.y; row < brect.y + brect.height; ++row) {
for (unsigned int col = brect.x; col < brect.x + brect.width; ++col) {
result = pointPolygonTest(contours[i], Point(col, row), false);
if (result == 1 || result == 0) {
segment.push_back(Point(col, row));
}
}
}
allSegments.push_back(segment);
}
output = Mat::zeros(src.size(), CV_8U);
int totalSize = output.rows*output.cols;
for (int segmentCount = 0; segmentCount < allSegments.size(); ++segmentCount) {
vector<Point> segment = allSegments[segmentCount];
if(segment.size() > totalSize*threshSize){
for (int idx = 0; idx < segment.size(); ++idx) {
output.at<uchar>(segment[idx].y, segment[idx].x) = 255;
}
}
}
return output;
}
I would suggest using morphological opening to get rid of the smaller blobs, followed by a morphological hole filling.