本文实例为大家分享了OpenCV计算图像的水平和垂直积分投影的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#include <cv.h>
#include <highgui.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
int main()
{
IplImage * src=cvLoadImage( "lena.jpg" ,0);
//cvSmooth(src,src,CV_BLUR,3,3,0,0);
cvThreshold(src,src,50,255,CV_THRESH_BINARY_INV);
IplImage* paintx=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
IplImage* painty=cvCreateImage( cvGetSize(src),IPL_DEPTH_8U, 1 );
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;
CvScalar s,t;
for (x=0;x<src->width;x++)
{
for (y=0;y<src->height;y++)
{
s=cvGet2D(src,y,x);
if (s.val[0]==0)
v[x]++;
}
}
for (x=0;x<src->width;x++)
{
for (y=0;y<v[x];y++)
{
t.val[0]=255;
cvSet2D(paintx,y,x,t);
}
}
for (y=0;y<src->height;y++)
{
for (x=0;x<src->width;x++)
{
s=cvGet2D(src,y,x);
if (s.val[0]==0)
h[y]++;
}
}
for (y=0;y<src->height;y++)
{
for (x=0;x<h[y];x++)
{
t.val[0]=255;
cvSet2D(painty,y,x,t);
}
}
cvNamedWindow( "二值图像" ,1);
cvNamedWindow( "垂直积分投影" ,1);
cvNamedWindow( "水平积分投影" ,1);
cvShowImage( "二值图像" ,src);
cvShowImage( "垂直积分投影" ,paintx);
cvShowImage( "水平积分投影" ,painty);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
cvReleaseImage(&paintx);
cvReleaseImage(&painty);
return 0;
}
|
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/hero_myself/article/details/49667613