- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。
这段代码加了点就结束了,没找到解决办法。
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/legacy/compat.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <cmath>
#include <list>
using namespace cv;
using namespace std;
//this method to find the 8-neighbors of a point from image
vector<Point> search8Neighbor(cv::Mat mat,Point startPoint)
{
vector<Point> segment;
Point p;
//uchar p1 = mat.at<uchar>(startPoint.x, startPoint.y);
uchar p2 = mat.at<uchar>(startPoint.x-1, startPoint.y);
uchar p3 = mat.at<uchar>(startPoint.x-1, startPoint.y+1);
uchar p4 = mat.at<uchar>(startPoint.x, startPoint.y+1);
uchar p5 = mat.at<uchar>(startPoint.x+1, startPoint.y+1);
uchar p6 = mat.at<uchar>(startPoint.x+1, startPoint.y);
uchar p7 = mat.at<uchar>(startPoint.x+1, startPoint.y-1);
uchar p8 = mat.at<uchar>(startPoint.x, startPoint.y-1);
uchar p9 = mat.at<uchar>(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<Point> traceLine(Mat img , Point peak)
{
vector<Point> vect1;
vector<Point> vect2;
img.at<uchar>(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<Point> vectN;
vectN=search8Neighbor(img,p);
vector<Point>::iterator it;
it = vect1.begin();
//cout<<vectN.size()<<endl;
for(int i=0;i<vectN.size();i++)
{
img.at<uchar>(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())
return -1;
cv::Mat img;
cv::threshold(im, img, 155, 255, CV_THRESH_BINARY);
vector<Point> vect1;
Point p;
p.x=20;
p.y=30;
if(mat.at<uchar>(p.x-1, p.y)==255)
vect1=traceLine(img,p);
imshow("image",im);
cv::waitKey();
return 0 ;
}
最佳答案
Mat 以矩阵的正常行/列方式进行索引,因此您需要 mat.at<uchar>(y, x)
, 不是 mat.at<uchar>(x, y)
, 否则会造成混淆。
你在哪里:
vector<Point>::iterator it;
it = vect1.begin();
...
for(...)
vect1.insert(it,vectN[i]);
如果插入导致缓冲区被重新分配,这将中断,因为它随后指向一些已被释放的内存,或者可能重新分配给其他东西。而是使用
vect1.insert(vect1.begin(),vectN[i]);
(这将对您的程序似乎执行的操作给出略微不同的顺序)或使用 push_back()
真正获得广度优先行为。
最大的问题在于 search8Neighbor()。你在哪里uchar p2 = mat.at<uchar>(startPoint.x-1, startPoint.y);
如果 startPoint 指向图像边缘的像素会怎样?您将引用图像外部的像素,并且程序将徘徊,直到发生异常。在这里,您需要检查您在图像中的位置,并确保附近没有包含来自图像外部的像素。
编辑我不相信如果像我说的那样改变代码就不会工作。我已经实现了修复并且它有效:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/legacy/compat.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <cmath>
#include <list>
using namespace cv;
using namespace std;
// Find the 8-neighbors of a point from image.
vector<Point> search8Neighbor(cv::Mat mat,Point startPoint)
{
vector<Point> neighbors;
vector<Point> 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<Point>::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<uchar>(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<Point> traceLine(Mat img , Point peak)
{
vector<Point> vect1;
vector<Point> vect2;
img.at<uchar>(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<Point> vectN;
vectN = search8Neighbor(img, p);
//cout<< " vectN.size()=" << vectN.size()<<endl;
for(int i = 0; i < int(vectN.size()); ++i)
{
img.at<uchar>(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())
return -1;
cv::Mat img;
cv::threshold(im, img, 155, 255, CV_THRESH_BINARY);
imshow("thresholded image",im);
vector<Point> vect1;
Point p(3, 32);
uchar u = img.at<uchar>(p.x, p.y);
if(img.at<uchar>(p) == 255)
vect1 = traceLine(img, p);
Mat output(im.rows, im.cols, CV_8UC3, Scalar(0, 0, 0));
vector<Point>::iterator it;
for(it = vect1.begin(); it < vect1.end(); ++it)
{
Vec3b green(0, 255, 0);
output.at<Vec3b>(*it) = green;
}
imshow("output", output);
cv::waitKey();
return 0 ;
}
关于c++ - 曲线/路径骨架二值图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622855/
我只是想知道需要什么样的计算/编程语言/框架来生成图像,例如 http://www.erdas.com/ 中的图像。 ? 以编程方式,如何生成一般空间分析图像? ps:我大部分时间都在使用java。
我尝试在我的 grails 项目(Mac OS X 上的 1.1.1)中使用一些图像处理插件或 java 库:imageTools 插件、imageJ、awt 库等。每次我从路径打开/获取图像以启动进
我有一个项目,我必须以多种方式处理图像。我陷入了像素化的困境。 对于像素化,我必须采用一组 10x10 像素并返回一个单独平均 RGB 颜色的单元格。目前我在运行程序中得到的只是一个红色图像。谢谢您的
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这是一项作业,因为我是Python编程新手,所以我付出了很大的努力: 我正在运行以下函数,它接受图像和短语(空格将被删除,因此只有文本)作为参数,我已经获得了所有导入和预处理代码,我只需要实现这个函数
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我需要一种简单易学且快速的方法来从背景图像、文本生成图像,然后保存为 JPEG 格式。 您有什么建议?有关于此的任何图书馆或教程吗?重要的标准是简单。 最佳答案 在 .Net 3.5/4 中,您还可以
我正在构建一个夜视应用程序,但我没有找到任何有用的算法可以应用于黑暗图像以使其清晰。任何人请给我一些好的算法。 提前致谢 最佳答案 由于 iphone 镜头和传感器的尺寸,无论您做什么,都会有很多噪音
所以我为游戏制作了这个程序,需要帮助让它更自动化一些。 程序接收图像然后显示它。我正在对 OpenGL 中的纹理执行此操作。当我截取游戏截图时,它通常约为 700x400。我将高度和宽度输入到我的程序
我想更改图像中像素的值,为此我需要将图像存储为矩阵。我怎样才能完成这项工作?请指导。 最佳答案 BufferedImage image = ImageIO.read(..); image.setRGB
概述: 我正在做一个视频创作项目。我使用的技术有:imageMagick、php、ffmpeg。 当前状态: 目前,该项目能够使用图像和文本以及很少的基本过渡来创建视频。我这样做的方式是使用 imag
我正在创建 facebook 应用程序,其中我将用户图像作为背景图像,并有一个用户可以四处移动的默认大写图像。用户将叠加图像(一顶帽子)放在正确的位置后,他点击保存 这就是我感到震惊的地方,我想知道如
我正在尝试编写一个 JavaScript 程序,通过在图像上放置三个垂直条纹来修改图像。左边三分之一是红色条纹,中间是绿色条纹,右边三分之一是蓝色条纹。 这是我试图实现的算法:1. 从您要更改的图像开
目前,我正在尝试通过图像分割方法将面部和头发修剪在一起,然后将所有非彩色像素设置为透明,然后尝试使用Binary Threshold技术和Adaptive Threshold。但是我得到了不希望的结果
我必须使此图像Book Image To Process的页面标题为:“单元3:主动学习的秘诀”,使其成为图像中的唯一页面 为此,我需要删除也在图像中的其他页面的一部分 我需要编写一个通用代码,可以对
我正在研究一个问题,其中我缩小图像的尺寸,在缩小图像中找到类似于二进制图像的有趣点。现在我只想放大在缩小图像中找到的有趣点(即白色像素点),而不是放大整个图像然后找到有趣的点。哪种技术可以最好地用于此
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 上个月关闭。 I
嗨,我需要编写一个程序,从灰度图像中删除分界(图像中带有文本) 我阅读了有关阈值和模糊的信息,但我仍然不知道该怎么做。 我的图像是这样的希伯来文本图像: 我需要删除分界线(假设分界线是图像中的最小元素
我正在做一个带有深度图像的项目。但是我的深度相机有噪音和像素读取失败的问题。有一些点和轮廓(尤其是边缘)的值为零。如何忽略这个零值并将其与周围的值混合? 我试过 dilation和 erosion (
我正在做一个大学项目,我需要在java中处理图像。前段时间我在数学实验室工作,这很容易,所以我想知道是否存在任何可以让我玩像素值、颜色(按像素)、RGB 模型、灰度图像等的 java 库。 最佳答案
我是一名优秀的程序员,十分优秀!