sách gpt4 ăn đã đi

c++ - Xử lý ảnh nhị phân khung đường cong/đường dẫn

In lại 作者:太空狗 更新时间:2023-10-29 21:45:51 29 4
mua khóa gpt4 giày nike

我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。

nhập mô tả hình ảnh ở đây

这段代码加了点就结束了,没找到解决办法。

 #include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include
#include
#include
#include
#include
using namespace cv;
sử dụng không gian tên std;

//this method to find the 8-neighbors of a point from image
vector search8Neighbor(cv::Mat mat,Point startPoint)
{
vector segment;
Point p;
//uchar p1 = mat.at(startPoint.x, startPoint.y);
uchar p2 = mat.at(startPoint.x-1, startPoint.y);
uchar p3 = mat.at(startPoint.x-1, startPoint.y+1);
uchar p4 = mat.at(startPoint.x, startPoint.y+1);
uchar p5 = mat.at(startPoint.x+1, startPoint.y+1);
uchar p6 = mat.at(startPoint.x+1, startPoint.y);
uchar p7 = mat.at(startPoint.x+1, startPoint.y-1);
uchar p8 = mat.at(startPoint.x, startPoint.y-1);
uchar p9 = mat.at(startPoint.x-1, startPoint.y-1);

//if(p1==255) segment.push_back(startPoint.x, startPoint.y);
if (p2 == 255) {
p.x=startPoint.x-1;
p.y=startPoint.y;
segment.push_back(p);
}
if(p3==255) {
p.x=startPoint.x-1;
p.y=startPoint.y+1;
segment.push_back(p);
}

if(p4==255) {
p.x=startPoint.x;
p.y=startPoint.y+1;
segment.push_back(p);
}

if(p5==255) {
p.x=startPoint.x+1;
p.y=startPoint.y+1;
segment.push_back(p);
}

if(p6==255) {
p.x=startPoint.x+1;
p.y=startPoint.y;
segment.push_back(p);
}
if(p7==255) {
p.x=startPoint.x+1;
p.y=startPoint.y-1;
segment.push_back(p);
}
if(p8==255){
p.x=startPoint.x;
p.y=startPoint.y-1;
segment.push_back(p);
}

if(p9==255) {
p.x=startPoint.x-1;
p.y=startPoint.y-1;
segment.push_back(p);
}

return segment ;

}
//this method return a vector of points from a skeleton that contains all the points
// between a start point "peak" and an end point
//this method use the idea of Breadth first search
vector traceLine(Mat img , Point peak)
{
vector vect1;
vector vect2;
img.at(peak.x,peak.y)=0;//
vect1.push_back(peak);//add peak to vect1
while(vect1.size() != 0)
{
Point p=vect1[0];
vect1.erase(vect1.begin());
vect2.push_back(p);
vector vectN;
vectN=search8Neighbor(img,p);
vector::iterator it;
it = vect1.begin();
//cout<<><>
for(int i=0;i<>
{
img.at(vectN[i].x,vectN[i].y)=0;
vect1.insert(it,vectN[i]);

}
}
return vect2;
}

int main( int argc, char** argv )
{
cv::Mat im = cv::imread("aa.jpg",0);
if (im.empty())
trả về -1;
cv::Mat img;
cv::threshold(im, img, 155, 255, CV_THRESH_BINARY);
vector vect1;
Point p;
p.x=20;
p.y=30;
if(mat.at(p.x-1, p.y)==255)
vect1=traceLine(img,p);
imshow("image",im);
cv::waitKey();
return 0 ;
}

câu trả lời hay nhất

Mat 以矩阵的正常行/列方式进行索引,因此您需要 mat.at(y, x) , 不是 mat.at(x, y) , 否则会造成混淆。

你在哪里:

vector::iterator it;
it = vect1.begin();
...
for(...)
vect1.insert(it,vectN[i]);

如果插入导致缓冲区被重新分配,这将中断,因为它随后指向一些已被释放的内存,或者可能重新分配给其他东西。而是使用

 vect1.insert(vect1.begin(),vectN[i]);

(这将对您的程序似乎执行的操作给出略微不同的顺序)或使用 push_back()真正获得广度优先行为。

最大的问题在于 search8Neighbor()。你在哪里uchar p2 = mat.at(startPoint.x-1, startPoint.y);如果 startPoint 指向图像边缘的像素会怎样?您将引用图像外部的像素,并且程序将徘徊,直到发生异常。在这里,您需要检查您在图像中的位置,并确保附近没有包含来自图像外部的像素。

biên tập我不相信如果像我说的那样改变代码就不会工作。我已经实现了修复并且它有效:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include
#include
#include
#include
#include
using namespace cv;
sử dụng không gian tên std;

// Find the 8-neighbors of a point from image.
vector search8Neighbor(cv::Mat mat,Point startPoint)
{
vector neighbors;
vector offsets;
offsets.push_back(Point(-1, 0)); // p2
offsets.push_back(Point(-1, 1)); // p3
offsets.push_back(Point( 0, 1)); // p4
offsets.push_back(Point( 1, 1)); // p5
offsets.push_back(Point( 1, 0)); // p6
offsets.push_back(Point( 1, -1)); // p7
offsets.push_back(Point( 0, -1)); // p8
offsets.push_back(Point(-1, -1)); // p9

vector::iterator it;
for(it = offsets.begin(); it < offsets.end(); ++it)
{
if(!((it->x < 0 && startPoint.x == 0)
|| (it->y < 0 && startPoint.y == 0)
|| (it->x > 0 && startPoint.x == mat.cols - 1)
|| (it->y > 0 && startPoint.y == mat.rows - 1)))
{
Point p(startPoint + *it);
if(mat.at(p) == 255)
neighbors.push_back(p);
}
}

return neighbors;
}

//this method return a vector of points from a skeleton that contains all the points
// between a start point "peak" and an end point
//this method use the idea of Breadth first search
vector traceLine(Mat img , Point peak)
{
vector vect1;
vector vect2;
img.at(peak.y, peak.x)=0;//
vect1.push_back(peak);//add peak to vect1
while(vect1.size() != 0)
{
Point p=vect1[0];
vect1.erase(vect1.begin());
vect2.push_back(p);
vector vectN;
vectN = search8Neighbor(img, p);

//cout<< " vectN.size()=" << vectN.size()<<>

for(int i = 0; i < int(vectN.size()); ++i)
{
img.at(vectN[i].y, vectN[i].x)=0;
vect1.push_back(vectN[i]);
}
}
return vect2;
}

int main( int argc, char** argv )
{
cv::Mat im = cv::imread("aa.jpg",0);
if (im.empty())
trả về -1;
cv::Mat img;
cv::threshold(im, img, 155, 255, CV_THRESH_BINARY);
imshow("thresholded image",im);
vector vect1;

Point p(3, 32);
uchar u = img.at(p.x, p.y);
if(img.at(p) == 255)
vect1 = traceLine(img, p);

Mat output(im.rows, im.cols, CV_8UC3, Scalar(0, 0, 0));
vector::iterator it;

for(it = vect1.begin(); it < vect1.end(); ++it)
{
Vec3b green(0, 255, 0);
output.at(*it) = green;
}
imshow("output", output);
cv::waitKey();
return 0 ;
}

关于c++ - 曲线/路径骨架二值图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622855/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress