OpenCV code doesn't work on specific image - c++

I am trying to run the followin code (based on this page) on an image, but it doesn't work:
Mat src=imread("img.jpg",1);
Mat tmp,thr;
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,200,255,THRESH_BINARY_INV);
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));//Ceate Mat to draw contour
int box_w=10; // Define box width here
int box_h=10; // Define box height here
int threshold_perc=25; //perceantage value for eliminating the box according to pixel count inside the box
int threshold=(box_w*box_h*threshold_perc)/100;
findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //Find contour
for( int i = 0; i< contours.size(); i++ ){
drawContours( dst,contours, i, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw contour with thickness = filled
Rect r= boundingRect(contours[i]); // Find bounding rect
// Scan the image with in bounding box
for(int j=r.x;j<r.x+r.width;j=j+box_w){
for(int k=r.y;k<r.y+r.height;k=k+box_h){
Rect roi_rect(j,k,box_w,box_h);
Mat roi = dst(roi_rect);
int count = countNonZero(roi);
if(count > threshold)
rectangle(src, roi_rect, Scalar(255,0,0),1,8,0 );
}
}
}
imshow("src",src);
waitKey();
It works fine for any normal image, but for the images below, it either breaks or doesn't find the contour and draws boxes all over the image.
It says:
Unhandled exception at 0x00007FF9A72DA388 in test2.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000FECC9DEAC0.
It breaks and points to here:
inline
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
in mat.inl.hpp.
What is wrong with my image? I have changed it from Gray-scale to RGB, but didn't help.
On the following image, it works fine:

As I commented, you're trying to access a region of the image that doesn't exist by using a rectangle of fixed size.
By intersecting the roi with the rectangle, you can avoid this problem:
Mat roi = dst(roi_rect & r);

The problem was that in the first images, the contour gets close to the boundaries of the image and in the bottom for loop of the program, it exceeds the coordinates. It was fixed with this:
// Scan the image with in bounding box
for (int j = r.x;j<r.x + r.width;j = j + box_w) {
for (int k = r.y;k<r.y + r.height;k = k + box_h) {
Rect roi_rect(j, k, box_w, box_h);
if (j + box_w < dst.cols && k + box_h < dst.rows)
{
Mat roi = dst(roi_rect);
int count = countNonZero(roi);
if (count > threshold)
rectangle(src, roi_rect, Scalar(0,0,255), 1, 8, 0);
}
}
}

Related

Get Pixel Values along several lines from a mask which is tilted

currently I have segmented the object (rectangular) and now I want to create a Line profile. I dont know how to get along this line.
detected object
Aim is to get this:
object with lines
Update 14:25:
I know already the angle from the bounding rect and used this to calculate the shift in y-direction in order to rearrange the values to a new mat so that I only need go through the matrix to get a line profile.
Here my Code, but the rearrangement did not work.
Mat imgIn(SizeY, SizeX, CV_16U, &Wire[0]),
imgOut(SizeY, SizeX, CV_16U, Scalar(0)),
temp, drawing, mask, lineProfile(SizeY, SizeX, CV_16U, Scalar(0));
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Point center;
char buffer[100];
bool found = false;
int rect_no (0);
double angle(0.0);
// Detecting outer contours
temp = ::adaptiveThreshold(imgIn, SizeY, SizeX, kernelSize, thresh, 0);
// Find contours
findContours(temp, contours, hierarchy, CV_RETR_TREE, CHAIN_APPROX_TC89_KCOS, Point(0, 0) );
/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
for( int i = 0; i < contours.size(); i++ ) minRect[i] = minAreaRect( Mat(contours[i]) );
// Draw contours + rotated rects
drawing = Mat::zeros(temp.size(), CV_8U );
Point2f rect_points[4];
for( int i = 0; i< minRect.size(); i++ ){
if((float)minRect[i].boundingRect().height/(float)minRect[i].boundingRect().width > 3.0 && (float)minRect[i].boundingRect().height/(float)minRect[i].boundingRect().width < 4.9){
// rotated rectangle
minRect[i].points(rect_points);
for(int j = 0; j < 4; j++) line(drawing, rect_points[j], rect_points[(j+1)%4], Scalar(255), 1, 8);
//found = minRect[i].boundingRect().contains(Point(459, 512));
if(minRect[i].boundingRect().area() > 1000)
rect_no = i;
}
}
center = computeCentroid(drawing);
cv::floodFill(drawing, center, cv::Scalar(255));
drawing.convertTo(imgOut, CV_16U, 257.0);
imgIn.copyTo(imgOut, drawing);
// Calculate Wire SR_min
// Get angle of Wire
angle = (90 - (-1 * minRect[rect_no].angle))*(CV_PI/180);
for(int i = 0;i < SizeY;i++){
for(int j = 0;j < SizeX;j++){
if(imgOut.at<ushort>(i, j) != 0)
lineProfile.at<ushort>(i, j) = imgOut.at<ushort>((unsigned short)(i/cos(angle)), j);
}
}
for(int i = 0;i < SizeY;i++){
for(int j = 0;j < SizeX;j++){
*Wire++ = lineProfile.at<ushort>(i, j);//imgOut.at<ushort>(i, j);
}
}
If you know the coordinates of the beginning and the end of your line, getting the values at each point on the line should be easy with OpenCV's LineIterator. Feed it your image and your two points and let it work its magic.
If you are able to binarize the detected object image , then you could possibly able to apply Houghlines function of OpenCv . You can find it in the below link
http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html

How to fill contours that touch the image border?

Say I have the following binary image created from the output of cv::watershed():
Now I want to find and fill the contours, so I can separate the corresponding objects from the background in the original image (that was segmented by the watershed function).
To segment the image and find the contours I use the code below:
cv::Mat bgr = cv::imread("test.png");
// Some function that provides the rough outline for the segmented regions.
cv::Mat markers = find_markers(bgr);
cv::watershed(bgr, markers);
cv::Mat_<bool> boundaries(bgr.size());
for (int i = 0; i < bgr.rows; i++) {
for (int j = 0; j < bgr.cols; j++) {
boundaries.at<bool>(i, j) = (markers.at<int>(i, j) == -1);
}
}
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(
boundaries, contours, hierarchy,
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE
);
So far so good. However, if I pass the contours acquired above to cv::drawContours() as below:
cv::Mat regions(bgr.size(), CV_32S);
cv::drawContours(
regions, contours, -1, cv::Scalar::all(255),
CV_FILLED, 8, hierarchy, INT_MAX
);
This is what I get:
The leftmost contour was left open by cv::findContours(), and as a result it is not filled by cv::drawContours().
Now I know this is a consequence of cv::findContours() clipping off the 1-pixel border around the image (as mentioned in the documentation), but what to do then? It seems an awful waste to discard a contour just because it happened to brush off the image's border. And anyway how can I even find which contour(s) fall in this category? cv::isContourConvex() is not a solution in this case; a region can be concave but "closed" and thus not have this problem.
Edit: About the suggestion to duplicate the pixels from the borders. The problem is that my marking function is also painting all pixels in the "background", i.e. those regions I'm sure aren't part of any object:
This results in a boundary being drawn around the output. If I somehow avoid cv::findContours() to clip off that boundary:
The boundary for the background gets merged with that leftmost object:
Which results in a nice white-filled box.
Solution number 1: use image extended by one pixel in each direction:
Mat extended(bgr.size()+Size(2,2), bgr.type());
Mat markers = extended(Rect(1, 1, bgr.cols, bgr.rows));
// all your calculation part
std::vector<std::vector<Point> > contours;
findContours(boundaries, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Mat regions(bgr.size(), CV_8U);
drawContours(regions, contours, -1, Scalar(255), CV_FILLED, 8, Mat(), INT_MAX, Point(-1,-1));
Note that contours were extracted from extended image, i.e. their x and y values are bigger by 1 from what they should be. This is why I use drawContours with (-1,-1) pixel offset.
Solution number 2: add white pixels from boundary of image to the neighbor row/column:
bitwise_or(boundaries.row(0), boundaries.row(1), boundaries.row(1));
bitwise_or(boundaries.col(0), boundaries.col(1), boundaries.col(1));
bitwise_or(boundaries.row(bgr.rows()-1), boundaries.row(bgr.rows()-2), boundaries.row(bgr.rows()-2));
bitwise_or(boundaries.col(bgr.cols()-1), boundaries.col(bgr.cols()-2), boundaries.col(bgr.cols()-2));
Both solution are half-dirty workarounds, but this is all I could think about.
Following Burdinov's suggestions I came up with the code below, which correctly fills all extracted regions while ignoring the all-enclosing boundary:
cv::Mat fill_regions(const cv::Mat &bgr, const cv::Mat &prospective) {
static cv::Scalar WHITE = cv::Scalar::all(255);
int rows = bgr.rows;
int cols = bgr.cols;
// For the given prospective markers, finds
// object boundaries on the given BGR image.
cv::Mat markers = prospective.clone();
cv::watershed(bgr, markers);
// Copies the boundaries of the objetcs segmented by cv::watershed().
// Ensures there is a minimum distance of 1 pixel between boundary
// pixels and the image border.
cv::Mat borders(rows + 2, cols + 2, CV_8U);
for (int i = 0; i < rows; i++) {
uchar *u = borders.ptr<uchar>(i + 1) + 1;
int *v = markers.ptr<int>(i);
for (int j = 0; j < cols; j++, u++, v++) {
*u = (*v == -1);
}
}
// Calculates contour vectors for the boundaries extracted above.
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(
borders, contours, hierarchy,
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE
);
int area = bgr.size().area();
cv::Mat regions(borders.size(), CV_32S);
for (int i = 0, n = contours.size(); i < n; i++) {
// Ignores contours for which the bounding rectangle's
// area equals the area of the original image.
std::vector<cv::Point> &contour = contours[i];
if (cv::boundingRect(contour).area() == area) {
continue;
}
// Draws the selected contour.
cv::drawContours(
regions, contours, i, WHITE,
CV_FILLED, 8, hierarchy, INT_MAX
);
}
// Removes the 1 pixel-thick border added when the boundaries
// were first copied from the output of cv::watershed().
return regions(cv::Rect(1, 1, cols, rows));
}

How to find No of inner Holes using cv::findcontours and hierarchy

I need to find the number of inner holes in the below image.i.e my ultimate requirement is to detect and find the area of round shape black holes alone using contour hierarchy in opencv.No need to use any other algorithms.
Based on this link Using hierarchy in findContours () in OpenCV? i tried but it won't worked.
is there any other method to find the no of holes in the image?
here with i have attached the sample image and code.Can anybody give idea to find the inner black holes alone using hierarchy.I don't have a much experience in contour hierarchy.Thanks in advance.
i used opencv c++ lib.
cv::Mat InputImage = imread("New Image.jpg");
int Err;
if(InputImage.empty() == 1)
{
InputImage.release();
cout<<"Error:Input Image Not Loaded"<<endl;
return 1;
}
cv::Mat greenTargetImage;
std::vector<cv::Mat> Planes;
cv::split(InputImage,Planes);
greenTargetImage = Planes[1];
cv::Mat thresholdImage = cv::Mat (greenTargetImage.size(),greenTargetImage.type());
cv::threshold(greenTargetImage,thresholdImage,128,255,THRESH_OTSU);
imwrite("thresholdImage.jpg",thresholdImage);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(thresholdImage,contours,hierarchy,cv::RETR_CCOMP,cv::CHAIN_APPROX_SIMPLE,cv::Point(-1,-1));
cout<<contours.size()<<endl;
cout<<hierarchy.size()<<endl;
int count = 0;
if (!contours.empty() && !hierarchy.empty())
{
for (int i = 0;i<contours.size();i++ )
{
if ( hierarchy[i][3] != -1)
{
cv::drawContours(InputImage,contours,i,CV_RGB(0,255,0),3);
count = count+1;
}
}
}
cout<<count<<endl; //No of inner holes in same level
imwrite("ContourImage.jpg",InputImage);
After applying this code i got the output count value is 11.But my requirement is count value should be 10 and also i need to draw only inner black holes alone not all boundaries of outer contours.Sorry for my english.
Try this code works fine for me using hierarchy.
The idea is simple, just consider the contour which doesn’t have child.
That is
hierarchy[i][2]= -1
code:-
Mat tmp,thr;
Mat src=imread("img.jpg",1);
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,200,255,THRESH_BINARY_INV);
namedWindow("thr",0);
imshow("thr",thr);
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
int count=0;
findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through each contour.
{
Rect r= boundingRect(contours[i]);
if(hierarchy[i][2]<0){
rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255),3,8,0);
count++;
}
}
cout<<"Numeber of contour = "<<count<<endl;
imshow("src",src);
imshow("contour",dst);
waitKey();
Result:-

Weird output using ROI (opencv C++)

I'm having difficulties using ROI using opencv c++.
I have a sequence of images which are stored in a vector. The vector image contained big blob and small blob. I want to remove the small blobs for every vector image. However, there is something wrong with the output result where if the small blobs in current vector image was removed, it will affect the blobs region for the next vector image (and previous vector image). Is there something wrong with ROI opencv c++? Below is sample code:
vector<Mat> finalImg;
for(unsigned int i = 0 ; i < srcImg.size(); i++) {
vector<vector<Point> > contoursFinal;
vector<Vec4i> hierarchyFinal;
Mat tempV_img;
srcImg[i].copyTo(tempV_img);
cv::findContours( tempV_img, contoursFinal, hierarchyFinal, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0,0) );
for(unsigned int j = 0; j < contoursFinal.size(); j++) {
Rect r = cv::boundingRect( contoursFinal[j] );
int heightChar = r.height;
/// Set image region of interest
cv::Rect ROI(r.x-1, r.y-1, r.width+2, r.height+2);
Mat srcImg_crop = srcImg[i](ROI);
cv::namedWindow("cropImg (bf)", 0);
cv::imshow("cropImg (bf)", srcImg_crop);
if(heightChar < srcImg[i].rows*0.90){
srcImg_crop.setTo(0);
}
cv::namedWindow("cropImg (af)", 0);
cv::imshow("cropImg (af)", srcImg_crop);
cv::waitKey(0);
if(cv::countNonZero(srcImg_crop) != 0) {
finalImg.push_back(srcImg_crop);
}
srcImg_crop.release();
}
cv::namedWindow("Sorted Final", 0);
cv::imshow("Sorted Final", finalImg[i]);
cv::waitKey(0);
contoursFinal.clear();
hierarchyFinal.clear();
}
Sorry all,
I just figured it out. Below shows the trick.
vector<Mat> tempV;
tempV.clear();
for(unsigned int i = 0 ; i < srcImg.size(); i++) {
Mat temp;
srcImg[i].copyTo(temp);
tempV.push_back(temp);
temp.release();
}
Instead of using srcImg[i]. I replace it with a new vector tempV[i]. Then it will not affect the previous as well as next vector image.

OpenCv 2.3 C - How to isolate object inside image

i have an image like:
i want to remove the black rows and cols round the number.
So i want that the result is:
i try this:
void findX(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;
CvMat data;
CvScalar maxVal=cvRealScalar(imgSrc->width * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->width; i++){
cvGetCol(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max= i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}
void findY(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;
CvMat data;
CvScalar maxVal=cvRealScalar(imgSrc->width * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->height; i++){
cvGetRow(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max=i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}
CvRect findBB(IplImage* imgSrc){
CvRect aux;
int xmin, xmax, ymin, ymax;
xmin=xmax=ymin=ymax=0;
findX(imgSrc, &xmin, &xmax);
findY(imgSrc, &ymin, &ymax);
aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin);
//printf("BB: %d,%d - %d,%d\n", aux.x, aux.y, aux.width, aux.height);
return aux;
}
So i use:
IplImage *my_image = cvLoad....
CvRect bb = findBB(my_image);
IplImage *new_image = cvCreateImage(cvSize(bb.width,bb.height), my_image->depth, 1);
cvShowImage("test",new_image);
it doesn't work good, cause i try to check if in new image there are black rows or cols and they are present. what can i do? can someone help me? (sorry for my english!)
One way to do it is to simply execute the bounding box technique to detect the digit, as illustrated by the image below:
Since your image is already processed the bounding box technique I use is a lot simpler.
After that procedure, all you really need to do is set the ROI (Region of Interest) of the original image to the area defined by the box to achieve the crop effect and isolate the object:
Notice that in the resulting image there is one extra row/column of pixels in the border that are not white. Well, they are not black either. That's because I didn't performed any threshold method to binarize the image to black and white. The code below demonstrates the bounding box technique being executed on a grayscale version of the image.
This is pretty much the roadmap to achieve what you want. For educational purposes I'm sharing the code I wrote using the C++ interface of OpenCV. I'm sure you are capable of converting it to the C interface.
#include <cv.h>
#include <highgui.h>
#include <vector>
int main(int argc, char* argv[])
{
cv::Mat img = cv::imread(argv[1]);
// Convert RGB Mat to GRAY
cv::Mat gray;
cv::cvtColor(img, gray, CV_BGR2GRAY);
// Store the set of points in the image before assembling the bounding box
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = gray.begin<uchar>();
cv::Mat_<uchar>::iterator end = gray.end<uchar>();
for (; it != end; ++it)
{
if (*it) points.push_back(it.pos());
}
// Compute minimal bounding box
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
// Draw bounding box in the original image (debug purposes)
//cv::Point2f vertices[4];
//box.points(vertices);
//for (int i = 0; i < 4; ++i)
//{
//cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
//}
//cv::imshow("box", img);
//cv::imwrite("box.png", img);
// Set Region of Interest to the area defined by the box
cv::Rect roi;
roi.x = box.center.x - (box.size.width / 2);
roi.y = box.center.y - (box.size.height / 2);
roi.width = box.size.width;
roi.height = box.size.height;
// Crop the original image to the defined ROI
cv::Mat crop = img(roi);
cv::imshow("crop", crop);
cv::imwrite("cropped.png", crop);
cvWaitKey(0);
return 0;
}