OpenCV: Converting from a Contour Tree to a Contour - cvContourFromContourTree() - opencv

I have a pointer to a CvContourTree and I wish to derive the associated contour from this.
I have tried to use the function that will do this -
cvContourFromContourTree(const CvContourTree* tree, CvMemStorage* storage, CvTermCriteria criteria )
but it is giving me an error:
'Unhandled exception at 0x1005567f in Matching_Hierarchial.exe: 0xC0000005:
Access violation reading location 0x00000002.'
I have defined the CvTermCriteria as follows:
CvTermCriteria termcrit = cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS ,5,1);
Can someone please provide some sample code of how to convert a contour to contour tree and then back to a contour again. I would be extremely grateful for help in this matter.
Thanks,
Conor
Thanks for your fast response. Please see the attched code segment. I have taken in an image from my project folder, converted it to binary. I have then found the contours. Using an arbitrary contour, I simplified its complexity via polygon approximation. I construct a contour tree from this contour (I am confident that this is working ok as I have tested this contour tree against a similar one using cvMatchContourTrees() and gotten favourable outcomes). However despite reading all I could find on the function and your post, I cannot convert from the contour tree back to the contour structure.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include <iostream>
using namespace std;
#define CVX_RED CV_RGB(0xff,0x00,0x00)
#define CVX_BLUE CV_RGB(0x00,0x00,0xff)
int _tmain(int argc, _TCHAR* argv[])
{
// define input image
IplImage *img1 = cvLoadImage("SHAPE1.jpg",0);
// define and construct binary image of input image
IplImage *imgEdge1 = cvCreateImage(cvGetSize(img1),IPL_DEPTH_8U,1);
cvThreshold(img1,imgEdge1,155,255,CV_THRESH_BINARY);
// define and zero image to place polygon image
IplImage *dst1 = cvCreateImage(cvGetSize(img1),IPL_DEPTH_8U,1);
cvZero(dst1);
// display ip and thresholded image
cvNamedWindow("img1",1);
cvNamedWindow("thresh1",1);
cvShowImage("img1",img1);
cvShowImage("thresh1",imgEdge1);
// find all the contours of the image
CvSeq* contours1 = NULL;
CvMemStorage* storage1 = cvCreateMemStorage();
int numContour1 = cvFindContours(imgEdge1,storage1,&contours1,sizeof(CvContour),CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);
cout<<"number of contours"<<numContour1<<endl;
// extract a contour of interest
CvSeq* poly_approx1 = contours1->v_next; // interested in vertical level becaue tree structure
// CALCULATE PERIMETER
double perimeter1 = cvArcLength((CvSeq*)poly_approx1,CV_WHOLE_SEQ,-1);
// CREATE POLYGON APPROXIMATION -
// NB: CANNOT USE 'CV_CHAIN_CODE'ARGUEMENT IN THE cvFindContours() call
CvSeq* polySeq1 = cvApproxPoly((CvSeq*)poly_approx1,sizeof(CvContour),storage1,CV_POLY_APPROX_DP,perimeter1*0.02,0);
// draw approximated polygon
cvDrawContours(dst1,polySeq1,cvScalar(255),cvScalar(255),0,3,8); // draw
// display polygon
cvNamedWindow("Poly Approx1",1);
cvShowImage("Poly Approx1",dst1);
// NOW WE HAVE A POLYGON APPROXIMATED CONTOUR
// CREATE A CONTOUR TREE
CvMemStorage *treeStorage1 = cvCreateMemStorage(0);
CvContourTree* tree1 = cvCreateContourTree((const CvSeq*)polySeq1,treeStorage1,0);
// TO RECONSTRUCT A CONTOUR FROM THE CONTOUR TREE
// CANNOT GET TO WORK YET...
CvMemStorage *stor = cvCreateMemStorage(0);
CvTermCriteria termcrit = cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS ,5,1); // more later
/* the next line will not compile */
CvSeq *contour_recap = cvContourFromContourTree(tree1,treeStorage1,termcrit);
cvWaitKey(0);
return 0;
}
Thanks again for any help or advice that you might be able to give. I assure you it's greatly appreciated.
Conor

Well, you are using the appropriate methods.
CvContourTree* cvCreateContourTree(
const CvSeq* contour,
CvMemStorage* storage,
double threshold);
This method will create the contour tree from a given sequence, which can then be used to compare two contours.
To convert a contour tree back to a sequence you will use the method you already posted, but remember to initialize the storage and create a TermCriteria(looks ok in your example):
storage = cvCreateMemStorage(0);
CvSeq* cvContourFromContourTree(
const CvContourTree* tree,
CvMemStorage* storage,
CvTermCriteria criteria);
So this steps should be ok for your conversion, and if there's nothing missing from your code than you should post more of it so we can find the mistake.

Related

removing watermark using opencv

I have used opencv and c++ to remove watermark from image using code below.
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <Windows.h>
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
bool debugFlag = true;
std::string path = "C:/test/";
for (const auto& entry : fs::directory_iterator(path))
{
std::string fileName = entry.path().string();
Mat original = imread(fileName, cv::IMREAD_COLOR);
if (debugFlag) { imshow("original", original); }
Mat inverted;
bitwise_not(original, inverted);
std::vector<Mat> channels;
split(inverted, channels);
for (int i = 0; i < 3; i++)
{
if (debugFlag) { imshow("chan" + std::to_string(i), channels[i]); }
}
Mat bwImg;
cv::threshold(channels[2], bwImg, 50, 255, cv::THRESH_BINARY);
if (debugFlag) { imshow("thresh", bwImg); }
Mat outputImg;
inverted.copyTo(outputImg, bwImg);
bitwise_not(outputImg, outputImg);
if (debugFlag) { imshow("output", outputImg); }
if (debugFlag) { waitKey(0); }
else { imwrite(fileName, outputImg); }
}
}
here is result original to removed watermark.
Now in previous image as you can see original image has orange/red watermark. I created a mask that would kill the watermark and then applied it to the original image (this pulls the grey text boundary as well). Another trick that helped was to use the red channel since the watermark is most saturated on red ~245). Note that this requires opencv and c++17
But now i want to remove watermark in new image which has similar watermark color as text image is given below as you can see some watermark in image sideway in chinese overlaping with text. how to achieve it with my current code any help is appreciated.
Two ideas to try:
1: The watermark looks "lighter" than the primary text. So if you create a grayscale version of the image, you may be able to apply a threshold that keeps the primary text and drops the watermark. You may want to add one pass of dilation on that mask before applying it to the original image as the grey thresh will likely clip your non-watermark characters a bit. (this may pull in too much noise from the watermark though, so test it)
2: Try using the opencv opening function. Your primary text seems thicker than the watermark, so you should be able to isolate it. Similarly after you create the mask of your keep text, dilate once and mask the original image.

Cannot stitch many images using Stitcher class in opencv c++

I want to stitch many images ( 25 ) in one single image of a straight surface of plastic part. Images look like this:
I am trying to use the Stitcher class form opencv. My code is here:
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result.jpg";
Mat img1, img2,img3,img4,img5,img6,img7, pano;
void printUsage();
//int parseCmdArgs(int argc, char** argv);
int main(int argc, char* argv[])
{
// Load images from HD.
img1 = imread("1.bmp");
img2 = imread("2.bmp");
img3 = imread("3.bmp");
img4 = imread("4.bmp");
// Put images into vector of images "imgs".
imgs.push_back(img1);
imgs.push_back(img2);
imgs.push_back(img3);
imgs.push_back(img4);
// Create stitcher instance and use stitch method with imgs.
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
stitcher.setPanoConfidenceThresh(0.8);
Stitcher::Status status = stitcher.stitch(imgs, pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << status << endl;
return -1;
}
imwrite(result_name, pano);
return 0;
}
I Always get an error saying : "Can't stitch images, error code = 1" so the system is saying that it needs more images. When debugging I see that images are properly loaded and then, vector imgs properly created. What can be the reasons? My calcuclation also last quite long (2 s)...
The stitcher module of OpenCV uses images features to create an exact alignment.
As a thumb rule, there should be around 20-30% overlap between the two images that have to be stitched. If there is no significant overlap between the camera fields, you won't have enough features between the image intersections. The images that you have given have quite a uniform background for the stitcher module to find sufficient features in the image intersection. You will need to look at increasing the features in the image intersection in order to align the images.

How to recognize digits from the analog counter?

I'm trying to read the following kWh numbers from the counter. The problem is the tesseract OCR doesn't recognize the analog digits.
The question is: will it be a better idea to make the photos of all of the digits (from 0 to 9) at different positions (I mean when digit is in the center, when it is a little at the top and the number 2 is appearing etc.) and to try image recognition instead of text recognition?
As far as I understood the difference is, that the image recognition compares the photos, while the text recognition... well I don't know...
Any advice?
Since the counter is not digital, but analog, we have problems at the transitions. The text/number recognition libraries can not recognize smth like that. The solution, that I've found is: Machine Learning.
Firstly I've made user to make the picture, where the numbers take 70-80% of the image (in order to remove the unneeded details).
Then I'm looking for parallel lines (if there are any) and cut the picture, that is between them (if the distance is big enough).
After that I'm filtering the picture (playing with contrast, brightness, set it grayscale) and then use the filter, that makes the image to contain only two colours (#000000 (black) and #ffffff (white)). In order to find the contours easier.
Then I find the contours by using Canny algorithm and filter them, by removing the unneeded details.
After that I use K-Nearest-Neighbour algorithm in order to recognize the digits.
But before I can recognize anything, I need to teach the algorithm, how the digits look like and what are they.
I hope it was useful!
Maybe you are not configuring tesseract right. I made a code using it that solves your problem:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <tesseract/baseapi.h>
#include <iostream>
using namespace cv;
int main(int argc, char** argv)
{
cv::Mat input = cv::imread("img.jpg");
//rectangle containing just the kWh numbers
Rect roi(358,327,532,89);
//convert to gray scale
Mat input_gray;
cvtColor(input(roi),input_gray,CV_BGR2GRAY);
//threshold image
Mat binary_img = input_gray>200;
//make a copy to use on findcontours
Mat copy_binary_img = binary_img.clone();
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//identify each blob in order to eliminate the small ones
findContours(copy_binary_img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
//filter blobs by their sizes
for (vector<vector<Point> >::iterator it = contours.begin(); it!=contours.end(); )
{
if (it->size()>20)
it=contours.erase(it);
else
++it;
}
//Erase blobs which have countour size smaller than 20
for( int i = 0; i< contours.size(); i++ )
{
drawContours( binary_img, contours, i, 0, -1, 8, hierarchy, 0, Point() );
}
//initialize tesseract OCR
tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetVariable("tessedit_char_whitelist", "0123456789-.");
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
//set input
tess.SetImage((uchar*)binary_img.data
, binary_img.cols
, binary_img.rows
, 1
, binary_img.cols);
// Get the text
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
waitKey();
return 0;
}

OpenCV GrabCut Algorithm example not working

I am trying to implement a grabcut algorithm in OpenCV using C++
I stumble upon this site and found a very simple way how to do it. Unfortunately, it seems like the code is not working for me
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
// Open another image
Mat image;
image= cv::imread("images/mango11a.jpg");
// define bounding rectangle
cv::Rect rectangle(50,70,image.cols-150,image.rows-180);
cv::Mat result; // segmentation result (4 possible values)
cv::Mat bgModel,fgModel; // the models (internally used)
// GrabCut segmentation
cv::grabCut(image, // input image
result, // segmentation result
rectangle,// rectangle containing foreground
bgModel,fgModel, // models
1, // number of iterations
cv::GC_INIT_WITH_RECT); // use rectangle
cout << "oks pa dito" <<endl;
// Get the pixels marked as likely foreground
cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);
// Generate output image
cv::Mat foreground(image.size(),CV_8UC3,cv::Scalar(255,255,255));
image.copyTo(foreground,result); // bg pixels not copied
// draw rectangle on original image
cv::rectangle(image, rectangle, cv::Scalar(255,255,255),1);
cv::namedWindow("Image");
cv::imshow("Image",image);
// display result
cv::namedWindow("Segmented Image");
cv::imshow("Segmented Image",foreground);
waitKey();
return 0;
}
Can anyone help me with this please? What is supposed to be the problem
PS: NO errors were printed while compiling.
check your settings again. I just executed the same tutorial and it worked fine for me.

OpenCV C++/Obj-C: goodFeaturesToTrack inside specific blob

Is there a quick solution to specify the ROI only within the contours of the blob I'm intereseted in?
My ideas so far:
Using the boundingRect, but it contains too much stuff I don't want to analyse.
Applying goodFeaturesToTrack to the whole image and then loop through the output coordinates to eliminate the once outside my blobs contour
Thanks in advance!
EDIT
I found what I need: cv::pointPolygonTest() seems to be the right thing, but I'm not sure how to implement it …
Here's some code:
// ...
IplImage forground_ipl = result;
IplImage *labelImg = cvCreateImage(forground.size(), IPL_DEPTH_LABEL, 1);
CvBlobs blobs;
bool found = cvb::cvLabel(&forground_ipl, labelImg, blobs);
IplImage *imgOut = cvCreateImage(cvGetSize(&forground_ipl), IPL_DEPTH_8U, 3);
if (found) {
vb::CvBlob *greaterBlob = blobs[cvb::cvGreaterBlob(blobs)];
cvb::cvRenderBlob(labelImg, greaterBlob, &forground_ipl, imgOut);
CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&greaterBlob->contour);
}
"polygon" contains the contour I need.
goodFeaturesToTrack is implemented this way:
- (std::vector<cv::Point2f>)pointsFromGoodFeaturesToTrack:(cv::Mat &)_image
{
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(_image,corners, 100, 0.01, 10);
return corners;
}
So next I need to loop through the corners and check each point with cv::pointPolygonTest(), right?
You can create a mask over your interest region:
EDIT
How to make a mask:
Make a mask;
Mat mask(origImg.size(), CV_8UC1);
mask.setTo(Scalar::all(0));
// here I assume your contour is extracted with findContours,
// and is stored in a vector<vector<Point>>
// and that you know which contour is the blob
// if it's not the case, use fillPoly instead of drawContour();
Scalar color(255,255,255); // white. actually, it's monchannel.
drawContours(mask, contours, contourIdx, color );
// fillPoly(Mat& img, const Point** pts, const int* npts,
// int ncontours, const Scalar& color)
And now you're ready to use it. BUT, look carefully at the result - I have heard about some bugs in OpenCV regarding the mask parameter for feature extractors, and I am not sure if it's about this one.
// note the mask parameter:
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners,
double qualityLevel, double minDistance,
InputArray mask=noArray(), int blockSize=3,
bool useHarrisDetector=false, double k=0.04 )
This will also improve the speed of your aplication - goodFeaturesToTrack eats a hoge amount of time, and if you apply it only on a smaller image, the overall gain is significant.

Resources