Finding distance between two curves
Posted GreenOpen专注图像处理
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Finding distance between two curves相关的知识,希望对你有一定的参考价值。
http://answers.opencv.org/question/129819/finding-distance-between-two-curves/
{
Mat img=imread("14878460214049233.jpg",IMREAD_GRAYSCALE);
imshow("test",img);
threshold(img,img,200,255,CV_THRESH_BINARY); // to delete some noise
imshow("test", img);
Mat labels;
connectedComponents(img,labels,8,CV_16U);
Mat result(img.size(),CV_32FC1,Scalar::all(0));
for (int i = 0; i <= 1; i++)
{
Mat mask1 = labels == 1+i;
Mat mask2 = labels == 1+(1-i);
Mat masknot;
bitwise_not(mask1,masknot);
imshow("masknot", masknot);
Mat dist;
distanceTransform(masknot,dist, DIST_L2,5,CV_8U);
imshow("distance float", dist/255);
dist.copyTo(result,mask2);
}
imshow("distance 1",result);
FileStorage fs("distCtr.yml",FileStorage::WRITE);
fs<<"Image"<<result;
fs.release();
waitKey();
SparseMat ms(result);
SparseMatConstIterator_<float> it = ms.begin<float>(),it_end = ms.end<float>();
Mat lig(result.rows,1,CV_8U,Scalar::all(0));
for (; it != it_end; it ++)
{
// print element indices and the element value
const SparseMat::Node* n = it.node();
if (lig.at<uchar>(n->idx[0])==0)
{
cout<< "("<<n->idx[0]<<","<<n->idx[1]<<") = " <<it.value<float>()<<"\\t";
lig.at<uchar>(n->idx[0])=1;
}
}
return 0;
}
int cv::connectedComponents | ( | InputArray | image, |
OutputArray | labels, | ||
int | connectivity = 8 , |
||
int | ltype = CV_32S |
||
) |
computes the connected components labeled image of boolean image
image with 4 or 8 way connectivity - returns N, the total number of labels [0, N-1] where 0 represents the background label. ltype specifies the output label image type, an important consideration based on the total number of labels or alternatively the total number of pixels in the source image.
- Parameters
-
image the 8-bit single-channel image to be labeled labels destination labeled image connectivity 8 or 4 for 8-way or 4-way connectivity respectively ltype output image label type. Currently CV_32S and CV_16U are supported.
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat img;
int threshval = 100;
static void on_trackbar(int, void*)
{
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
Mat labelImage(img.size(), CV_32S);
int nLabels = connectedComponents(bw, labelImage, 8);
std::vector<Vec3b> colors(nLabels);
colors[0] = Vec3b(0, 0, 0);//background
for(int label = 1; label < nLabels; ++label){
colors[label] = Vec3b( (rand()&255), (rand()&255), (rand()&255) );
}
Mat dst(img.size(), CV_8UC3);
for(int r = 0; r < dst.rows; ++r){
for(int c = 0; c < dst.cols; ++c){
int label = labelImage.at<int>(r, c);
Vec3b &pixel = dst.at<Vec3b>(r, c);
pixel = colors[label];
}
}
imshow( "Connected Components", dst );
}
static void help()
{
cout << "\\n This program demonstrates connected components and use of the trackbar\\n"
"Usage: \\n"
" ./connected_components <image(../data/stuff.jpg as default)>\\n"
"The image is converted to grayscale and displayed, another image has a trackbar\\n"
"that controls thresholding and thereby the extracted contours which are drawn in color\\n";
}
const char* keys =
{
"{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}"
};
int main( int argc, const char** argv )
{
CommandLineParser parser(argc, argv, keys);
if (parser.has("help"))
{
help();
return 0;
}
string inputImage ="twolines.jpg";
img = imread(inputImage.c_str(), 0);
if(img.empty())
{
cout << "Could not read input image file: " << inputImage << endl;
return -1;
}
namedWindow( "Image", 1 );
imshow( "Image", img );
namedWindow( "Connected Components", 1 );
createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar );
on_trackbar(threshval, 0);
waitKey(0);
return 0;
}
....
Mat tmp = labels == 1;
Mat tmp2 = labels == 2;
Mat tmp3 = labels == 3;
• CV_8S - 8-bit signed integers ( -128..127 )
• CV_16U - 16-bit unsigned integers ( 0..65535 )
• CV_16S - 16-bit signed integers ( -32768..32767 )
• CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
• CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
• CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )
{
Mat mask1 = labels == 1+i;
Mat mask2 = labels == 1+(1-i);
Mat masknot;
bitwise_not(mask1,masknot);
imshow("masknot", masknot);
Mat dist;
distanceTransform(masknot,dist, DIST_L2,5,CV_8U);
imshow("distance float", dist/255);
dist.copyTo(result,mask2);
}
SparseMatConstIterator_<float> it = ms.begin<float>(),it_end = ms.end<float>();
Mat lig(result.rows,1,CV_8U,Scalar::all(0));
for (; it != it_end; it ++)
{
// print element indices and the element value
const SparseMat::Node* n = it.node();
if (lig.at<uchar>(n->idx[0])==0)
{
cout<< "("<<n->idx[0]<<","<<n->idx[1]<<") = " <<it.value<float>()<<"\\t";
lig.at<uchar>(n->idx[0])=1;
}
}
以上是关于Finding distance between two curves的主要内容,如果未能解决你的问题,请参考以下文章
1184. Distance Between Bus Stops
783. Minimum Distance Between BST Nodes
LeetCode --- 1184. Distance Between Bus Stops 解题报告
783. Minimum Distance Between BST Nodes