2019-12-04 00:07:52 -0600
| received badge | ● Notable Question
(source)
|
2017-10-16 04:08:39 -0600
| received badge | ● Popular Question
(source)
|
2015-10-05 05:33:59 -0600
| commented question | Problem with Arclength function Thanks for your reply, I tried it, both closed and not closed. However, it didn't work. I guess it is because of the data storing structure in contour. Too many repeated pixels and the storing order is not strict neighbor to neighbor. I manually coded to calculate the length. |
2015-10-04 21:35:06 -0600
| asked a question | Problem with Arclength function I used Arclength to calculate curve length, but I found that the results are much larger than the real length. So is the perimeter for closed contour, I think. I wonder whether the area calculation is correct or not. |
2015-08-19 04:01:18 -0600
| commented question | Problem when I try to use findContour function I don't konw why I can't upload pictures. Thanks for your help! In fact, I tested some other sample images, there were no such problem, I guess maybe it is because that the contour is not smooth |
2015-08-19 03:15:41 -0600
| asked a question | Problem when I try to use findContour function When I want to detect contours of watershed results, interesting thing emerged.! Applied image
file:///C:/E/Figure/Test/marker.jpg
marker image
file:///C:/E/Figure/Test/ws.bmp
watershed result
file:///C:/E/Figure/Test/contours.jpg
contour result
Problem description: It is obvious that the contours of some shapes are not continuous but split into several different groups.
Code: /* findContour */
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/features2d/features2d.hpp>
#include <cstdio>
#include <fstream>
using namespace cv;
using namespace std;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat canny_output;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
}
int _tmain(int argc, _TCHAR* argv[])
{
/* watershed program has been omitted*/
// find contour
Mat wsgray(watershedim.rows,watershedim.cols,CV_8UC1), wscontour(watershedim.rows,watershedim.cols,CV_8UC3);
cvtColor(watershedim,wsgray,COLOR_BGR2GRAY);
imshow("wsgray",wsgray);
waitKey(0);
watershedim.copyTo(src);
wsgray.copyTo(src_gray);
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);
for(int k = 0; k < contours.size(); k++)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( wscontour, contours, k, color, 2, 8, hierarchy, 0, Point() );
}
imshow("wscontour", wscontour);
waitKey(0);
}
|
2015-05-03 02:08:38 -0600
| received badge | ● Scholar
(source)
|
2015-04-30 21:17:15 -0600
| commented question | Problem when calculating the fitted ellipse I posted all the codes, plz help me to check it, my opencv version is 2.4.11. The code start from the GetContour() function. |
2015-04-30 21:15:55 -0600
| received badge | ● Editor
(source)
|
2015-04-30 06:23:28 -0600
| commented question | Problem when calculating the fitted ellipse Absolutely, would you please explain the reason? |
2015-04-30 03:32:54 -0600
| asked a question | Problem when calculating the fitted ellipse I need to extract contour of shapes in images, and get the fitted ellipse. However, when I use the following code, the result is not very satisfying. #include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <fstream>
using namespace std;
using namespace cv;
extern void Threshold(Mat gray, Mat& binary);
extern void Not(Mat& im);
// Calculate the contour of connected region
// erease the image that is too large or too small
void getSizeContours(vector<vector<Point>> &contours)
{
int cmin = 10; // min contour length
int cmax = 10000; // max contour length
vector<vector<Point>>::const_iterator itc = contours.begin();
while(itc != contours.end())
{
if((itc->size()) < cmin || (itc->size()) > cmax)
{
itc = contours.erase(itc);
}
else ++ itc;
}
}
// Calculate min convex oval
void MinConvexOval(Mat gray, vector<vector<Point>> &contours, vector<Mat> oval_result)
{
Mat oval(gray.rows*3,gray.cols*3,CV_8UC3);
vector<vector<Point>>::const_iterator itc=contours.begin();
while(itc != contours.end())
{
double perimeter=arcLength(*itc,true);
double area= contourArea(*itc,false);
Rect rect=boundingRect(*itc);
CvBox2D box=minAreaRect(*itc);
Point pt1, pt2;
pt1.x=rect.x;
pt1.y=rect.y;
pt2.x=rect.x+rect.width;
pt2.y=rect.y+rect.height;
RotatedRect box2=fitEllipse(*itc);
ellipse(oval, box2, Scalar(255,0,255), 1, 8);
Point2f vertices[4];
box2.points(vertices);
for (int i = 0; i < 4; i++)
line(oval, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
++itc;
}
drawContours(oval,contours,-1, Scalar(0,255,255), 1);
imshow("oval",oval);
waitKey(0);
}
void cvText(Mat img, const char* text, int x, int y)
{
CvFont font;
double hscale = 1.0;
double vscale = 1.0;
int linewidth = 2;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX |CV_FONT_ITALIC,hscale,vscale,0,linewidth);
Scalar textColor =Scalar(0,255,255);
Point textPos =Point(x, y);
putText(img, text, textPos, CV_FONT_HERSHEY_COMPLEX, 1,textColor);
}
void GetContour(Mat grayim)
{
Mat binaryim;
Threshold(grayim, binaryim); // binaryzation
vector<vector<Point>> contours;
//CV_CHAIN_APPROX_NONE Get all of the pixels of the contours
findContours(binaryim, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
getSizeContours(contours);
vector<Mat> oval_result;
MinConvexOval(grayim,contours,oval_result);
}
Testing image: Result image: The fitting result of the right-down shape is wrong, could anyone help me, please? Thanks a lot! |