分类:
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
利用OPENCV计算图像的水平积分投影和垂直积分投影
在做图像处理时会经常需要接触到各种目标提取的方法,其中最常见的就是利用积分投影对目标进行提取分割,下面就直接上代码,通过代码来介绍一下如何获得图像的水平积分投影和垂直积分投影。该代码编写风格较为简单,适合刚入门学习opencv的新手。
- <span style="font-size:18px;">#include<opencv2\opencv.hpp>
- #include<stdio.h>
- using namespace cv;
- Mat VerticalProjection(Mat srcImage)//垂直积分投影
- {
- if (srcImage.channels() > 1)
- cvtColor(srcImage, srcImage, CV_RGB2GRAY);
- Mat srcImageBin;
- threshold(srcImage, srcImageBin, 120, 255, CV_THRESH_BINARY_INV);
- imshow("二值图", srcImageBin);
- int *colswidth = new int[srcImage.cols]; //申请src.image.cols个int型的内存空间
- memset(colswidth, 0, srcImage.cols * 4); //数组必须赋初值为零,否则出错。无法遍历数组。
- // memset(colheight,0,src->width*4);
- // CvScalar value;
- int value;
- for (int i = 0; i < srcImage.cols; i++)
- for (int j = 0; j < srcImage.rows; j++)
- {
- //value=cvGet2D(src,j,i);
- value = srcImageBin.at<uchar>(j, i);
- if (value == 255)
- {
- colswidth[i]++; //统计每列的白色像素点
- }
- }
- Mat histogramImage(srcImage.rows, srcImage.cols, CV_8UC1);
- for (int i = 0; i < srcImage.rows; i++)
- for (int j = 0; j < srcImage.cols; j++)
- {
- value = 255; //背景设置为白色。
- histogramImage.at<uchar>(i, j) = value;
- }
- for (int i = 0; i < srcImage.cols; i++)
- for (int j = 0; j < colswidth[i]; j++)
- {
- value = 0; //直方图设置为黑色
- histogramImage.at<uchar>(srcImage.rows - 1 - j, i) = value;
- }
- imshow(" 垂直积分投影图", histogramImage);
- return histogramImage;
- }
- Mat HorizonProjection(Mat srcImage)//水平积分投影
- {
- if (srcImage.channels() > 1)
- cvtColor(srcImage, srcImage, CV_RGB2GRAY);
- Mat srcImageBin;
- threshold(srcImage, srcImageBin, 120, 255, CV_THRESH_BINARY_INV);
- imshow("二值图", srcImageBin);
- int *rowswidth = new int[srcImage.rows]; //申请src.image.rows个int型的内存空间
- memset(rowswidth, 0, srcImage.rows * 4); //数组必须赋初值为零,否则出错。无法遍历数组。
- int value;
- for (int i = 0; i<srcImage.rows; i++)
- for (int j = 0; j<srcImage.cols; j++)
- {
- //value=cvGet2D(src,j,i);
- value = srcImageBin.at<uchar>(i, j);
- if (value == 255)
- {
- rowswidth[i]++; //统计每行的白色像素点
- }
- }
- Mat histogramImage(srcImage.rows, srcImage.cols, CV_8UC1);
- for (int i = 0; i<srcImage.rows; i++)
- for (int j = 0; j<srcImage.cols; j++)
- {
- value = 255; //背景设置为白色。
- histogramImage.at<uchar>(i, j) = value;
- }
- //imshow("d", histogramImage);
- for (int i = 0; i<srcImage.rows; i++)
- for (int j = 0; j<rowswidth[i]; j++)
- {
- value = 0; //直方图设置为黑色
- histogramImage.at<uchar>(i, j) = value;
- }
- imshow("水平积分投影图", histogramImage);
- delete[] rowswidth;//释放前面申请的空间
- return histogramImage;
- }
- int main()
- {
- Mat srcImage = imread("145.png");
- imshow("原图", srcImage);
- Mat VP;
- VP = VerticalProjection(srcImage);
- Mat HP;
- HP = HorizonProjection(srcImage);
- waitKey(0);
- return 0;
- }</span>
#include<opencv2\opencv.hpp>
#include<stdio.h>
using namespace cv;
Mat VerticalProjection(Mat srcImage)//垂直积分投影
{
if (srcImage.channels() > 1)
cvtColor(srcImage, srcImage, CV_RGB2GRAY);
Mat srcImageBin;
threshold(srcImage, srcImageBin, 120, 255, CV_THRESH_BINARY_INV);
imshow("二值图", srcImageBin);
int *colswidth = new int[srcImage.cols]; //申请src.image.cols个int型的内存空间
memset(colswidth, 0, srcImage.cols * 4); //数组必须赋初值为零,否则出错。无法遍历数组。
// memset(colheight,0,src->width*4);
// CvScalar value;
int value;
for (int i = 0; i < srcImage.cols; i++)
for (int j = 0; j < srcImage.rows; j++)
{
//value=cvGet2D(src,j,i);
value = srcImageBin.at<uchar>(j, i);
if (value == 255)
{
colswidth[i]++; //统计每列的白色像素点
}
}
Mat histogramImage(srcImage.rows, srcImage.cols, CV_8UC1);
for (int i = 0; i < srcImage.rows; i++)
for (int j = 0; j < srcImage.cols; j++)
{
value = 255; //背景设置为白色。
histogramImage.at<uchar>(i, j) = value;
}
for (int i = 0; i < srcImage.cols; i++)
for (int j = 0; j < colswidth[i]; j++)
{
value = 0; //直方图设置为黑色
histogramImage.at<uchar>(srcImage.rows - 1 - j, i) = value;
}
imshow(" 垂直积分投影图", histogramImage);
return histogramImage;
}
Mat HorizonProjection(Mat srcImage)//水平积分投影
{
if (srcImage.channels() > 1)
cvtColor(srcImage, srcImage, CV_RGB2GRAY);
Mat srcImageBin;
threshold(srcImage, srcImageBin, 120, 255, CV_THRESH_BINARY_INV);
imshow("二值图", srcImageBin);
int *rowswidth = new int[srcImage.rows]; //申请src.image.rows个int型的内存空间
memset(rowswidth, 0, srcImage.rows * 4); //数组必须赋初值为零,否则出错。无法遍历数组。
int value;
for (int i = 0; i<srcImage.rows; i++)
for (int j = 0; j<srcImage.cols; j++)
{
//value=cvGet2D(src,j,i);
value = srcImageBin.at<uchar>(i, j);
if (value == 255)
{
rowswidth[i]++; //统计每行的白色像素点
}
}
Mat histogramImage(srcImage.rows, srcImage.cols, CV_8UC1);
for (int i = 0; i<srcImage.rows; i++)
for (int j = 0; j<srcImage.cols; j++)
{
value = 255; //背景设置为白色。
histogramImage.at<uchar>(i, j) = value;
}
//imshow("d", histogramImage);
for (int i = 0; i<srcImage.rows; i++)
for (int j = 0; j<rowswidth[i]; j++)
{
value = 0; //直方图设置为黑色
histogramImage.at<uchar>(i, j) = value;
}
imshow("水平积分投影图", histogramImage);
delete[] rowswidth;//释放前面申请的空间
return histogramImage;
}
int main()
{
Mat srcImage = imread("145.png");
imshow("原图", srcImage);
Mat VP;
VP = VerticalProjection(srcImage);
Mat HP;
HP = HorizonProjection(srcImage);
waitKey(0);
return 0;
}
运行结果:
总的来说,对于计算图像的垂直或水平积分投影,最重要的还是对图像的二值化操作,一个合适的二值化操作,再通过积分投影往往就能比较容易提取出目标;一个垃圾的二值化操作是无法通过投影的方法提取出目标的。
-
顶
- 1
-
踩
- 0
- 上一篇编写头文件时需要注意的事
- 下一篇ini配置文件读写操作入门