C++ opencv与投影值
您好,我想使用高度投影在一些区域检测和我想向她显示的区域之间进行选择,我可以在代码中添加什么来仅显示具有两个白色峰值的二值图像?图片链接:以下代码基于对您的代码的一些假设:C++ opencv与投影值,c++,C++,您好,我想使用高度投影在一些区域检测和我想向她显示的区域之间进行选择,我可以在代码中添加什么来仅显示具有两个白色峰值的二值图像?图片链接:以下代码基于对您的代码的一些假设: #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include <highgui.h> int main(int argc, char *argv[]) { //
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
// src is 32bits
IplImage* src_original = cvLoadImage("picture.jpg",1);
//convert to grayscale! (very important)
IplImage *src = cvCreateImage( cvGetSize(src_original), IPL_DEPTH_8U, 1);
cvCvtColor( src_original, src, CV_RGB2GRAY );
// release the original image
cvReleaseImage(&src_original );
//paintx, painty son 8bits
IplImage* paintx=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
IplImage* painty=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
// this is going to set colour to black
cvZero(paintx);
cvZero(painty);
int* v=new int[src->width];
int* h=new int[src->height];
memset(v,0,src->width*4);
memset(h,0,src->height*4);
int x,y;
uchar s;
CvScalar t;
// counts pixels with bright<100 along each column ("black" pixels)
//
// | | | | | | |
// | | | | | | |
// v v v v v v v
for(x=0;x<src->width;x++)
{
for(y=0;y<src->height;y++)
{
s = (((uchar*)(src->imageData+src->widthStep*y))[x]); //changed because of 0xC0000005 return error
if(s < 100)
v[x]++;
}
}
// vertical projection
//( with this code, graphic grows up )
for(x=0;x<src->width;x++)
{
for(y=src->height-1; y >= src->height-v[x];y--)
{
t.val[0]=255;
cvSet2D(paintx,y,x,t);
}
}
// counts pixels with bright<100 along each row ("black" pixels)
//
// --------->
// --------->
// --------->
for(y=0;y<src->height;y++)
{
for(x=0;x<src->width;x++)
{
s = (((uchar*)(src->imageData+src->widthStep*y))[x]);
if(s < 100)
h[y]++;
}
}
// horizontal projection
// graphic grows from right to left
for(y=0;y<src->height;y++)
{
for(x=src->width-1; x>= src->width -h[y];x--)
{
t.val[0]=255;
cvSet2D(painty,y,x,t);
}
}
cvNamedWindow("projection_vertical",1);
cvShowImage("projection_vertical",paintx);
cvNamedWindow("projection_horiz",1);
cvShowImage("projection_horiz",painty);
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&src );
cvReleaseImage(&paintx );
cvReleaseImage(&painty );
return 0;
}
第一次编辑
我更改了代码,将其调整为您刚刚发布的图像
第二次编辑
对白色像素块进行计数
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
// src is 32bits
IplImage* src_original = cvLoadImage("picture.jpg",1);
//convert to grayscale! (very important)
IplImage *src = cvCreateImage( cvGetSize(src_original), IPL_DEPTH_8U, 1);
cvCvtColor( src_original, src, CV_RGB2GRAY );
// release the original image
cvReleaseImage(&src_original );
//paintx, painty son 8bits
IplImage* paintx=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
IplImage* painty=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
// this is going to set colour to white
CvScalar c; c.val[0]=255;
cvSet(paintx,c);
cvSet(painty,c);
int* v=new int[src->width];
int* h=new int[src->height];
memset(v,0,src->width*4);
memset(h,0,src->height*4);
int x,y;
int is_prevwhite; //used to remember a change of colour
uchar s;
CvScalar t;
// counts blocks of pixels with bright>100 along each column ("white" blocks)
//
// | | | | | | |
// | | | | | | |
// v v v v v v v
for(x=0;x<src->width;x++)
{
is_prevwhite = 0;
for(y=0;y<src->height;y++)
{
s = (((uchar*)(src->imageData+src->widthStep*y))[x]); //changed because of 0xC0000005 return error
if(s > 100)
if (!is_prevwhite){
v[x]++;
is_prevwhite = 1;
}
else
if (is_prevwhite)
is_prevwhite = 0;
}
}
// vertical projection
//( with this code, graphic grows up )
for(x=0;x<src->width;x++)
{
for(y=src->height-1; y >= src->height-v[x];y--)
{
t.val[0]=0;
cvSet2D(paintx,y,x,t);
}
}
// counts blocks of pixels with bright>100 along each row ("white" blocks)
//
// --------->
// --------->
// --------->
for(y=0;y<src->height;y++)
{
is_prevwhite = 0;
for(x=0;x<src->width;x++)
{
s = (((uchar*)(src->imageData+src->widthStep*y))[x]);
if(s > 100)
if (!is_prevwhite){
h[y]++;
is_prevwhite = 1;
}
else
if (is_prevwhite)
is_prevwhite = 0;
}
}
// horizontal projection
// graphic grows from right to left
for(y=0;y<src->height;y++)
{
for(x=src->width-1; x>= src->width -h[y];x--)
{
t.val[0]=0;
cvSet2D(painty,y,x,t);
}
}
cvNamedWindow("projection_vertical",1);
cvShowImage("projection_vertical",paintx);
cvNamedWindow("projection_horiz",1);
cvShowImage("projection_horiz",painty);
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&src );
cvReleaseImage(&paintx );
cvReleaseImage(&painty );
return 0;
}
如果要添加图像,需要将其上载到外部站点,例如imgur.com。然后复制直接链接并粘贴到此处。在没有星星的情况下,像这样做:*[A name]*ImageURL是第二幅图像的链接,因此就像在两幅图片中一样,如果图像包含一行白色像素,则它们有一个峰值如果它们包含两行白色像素,则它们有两个峰值,所以我想在我的代码中添加一些像这样的循环,如果图像图包含+/-两个峰值,它就是我需要的,如果没有,就在窗口中显示它。我很抱歉。我想我误解了这个问题。你的意思是:如果一行包含X个白色块,图形应该有一个长度为X的峰值?当我尝试使用这个时,它会使程序崩溃。抱歉,a.J当我尝试使用你的代码时,它会崩溃,请我添加一些图片,如果你能看到它们,它会直接获取名称文件。根据你们的截图:现在它计算黑色像素,而不是白色像素;图形以相反的方式增长^和<;颜色反转了,沿着列/行的黑色像素越多,投影中的白线长度越长,但是我需要对图像中的白行进行计数,这些是我的设置:OpenCV2.2,代码::块+明文,图片。JPG在可执行的目录中,项目是C++,尽管包含了