几何投影对简单二值图像的水平、垂直及对角线的投影

几何投影:对一幅简单二值图像的水平、垂直及对角线的投影。
(注:代码只适用于一些相对简单的二值图像,程序运行后生成的结果在文件夹里,VS里不会显示生成结果。)

#include"stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;

using namespace cv;

//建立三个函数分别来处理三个投影
void HProjection(Mat mat)
{
	Mat hmat;
	hmat.create(mat.rows, mat.cols, CV_32F);//存储方式不同




	IplImage pmat(mat), phmat(hmat);
	//初始化图像的像素值为255
	for (int i = 0; i<phmat.height; i++)
	{
		for (int j = 0; j<phmat.width; j++)
		{
			cvSetReal2D(&phmat, i, j, 255);
		}
	}

	for (int i = 0; i<pmat.height; i++)
	{
		int n = phmat.width - 1;
		for (int j = 0; j<pmat.width; j++)
		{
			if (cvGetReal2D(&pmat, i, j) == 0)
			{
				cvSetReal2D(&phmat, i, n--, 0);
			}
		}
	}
	/*Mat rmat(&phmat);
	imshow("HPrjection", rmat);
	cvWaitKey();*/
	//cvSaveImage("D:\\Documents\\Visual Studio 2010\\Projects\\Image projection\\hm.jpg", &phmat);
	imwrite("E:\\ckechengxiangmu\\hm.jpg", hmat);
}
void VProjection(Mat mat)
{
	Mat hmat;
	hmat.create(mat.rows, mat.cols, CV_32F);//存储方式不同
	IplImage pmat(mat), phmat(hmat);
	//初始化图像的像素值为255
	for (int i = 0; i<phmat.height; i++)
	{
		for (int j = 0; j<phmat.width; j++)
		{
			cvSetReal2D(&phmat, i, j, 255);
		}
	}
	for (int i = 0; i<pmat.width; i++)
	{
		int n = phmat.height - 1;
		for (int j = 0; j<pmat.height; j++)
		{
			if (cvGetReal2D(&pmat, j, i) == 0)
			{
				cvSetReal2D(&phmat, n--, i, 0);
			}
		}
	}
	//Mat rmat(&phmat);
	//imshow("VPrjection", rmat);
	//cvWaitKey();
	//cvSaveImage("D:\\Documents\\Visual Studio 2010\\Projects\\Image projection\\vm.jpg", &phmat);
	imwrite("E:\\ckechengxiangmu\\vm.jpg", hmat);
}
void XProjection(Mat mat)
{
	//45度投影比较复杂,代码如下,统计正方形
	Mat hmat;
	hmat.create(mat.rows, mat.cols, CV_32F);//存储方式不同
	IplImage pmat(mat), phmat(hmat);
	for (int i = 0; i<phmat.height; i++)
	{
		for (int j = 0; j<phmat.width; j++)
		{
			cvSetReal2D(&phmat, i, j, 255);
		}
	}
	for (int i = 0; i<1; i++)
	{
		for (int j = 0; j<pmat.width; j++)
		{
			int n = phmat.height - 1;
			for (int k = 0; k <= j; k++)
			{
				if (cvGetReal2D(&pmat, i + k, j - k) == 0)
				{
					cvSetReal2D(&phmat, n--, j, 0);
				}
			}
		}
	}

	for (int i = pmat.height - 1; i>pmat.height - 2; i--)
	{
		for (int j = pmat.width - 1; j>0; j--)
		{
			int n = phmat.height - 1;
			for (int k = 0; k <= pmat.width - 1 - j; k++)
			{
				if (cvGetReal2D(&pmat, i - k, j + k) == 0)
				{
					cvSetReal2D(&phmat, n--, j  - 1, 0);
				}
			}
		}
	}
	//Mat rmat(&phmat);
	//imshow("XPrjection", rmat);
	//cvWaitKey();
	//cvSaveImage("D:\\Documents\\Visual Studio 2010\\Projects\\Image projection\\xm.jpg", &phmat);
	imwrite("E:\\ckechengxiangmu\\xm.jpg", hmat);
}
int main()
{
	string strpath = "E:\\ckechengxiangmu\\11.jpg";
	Mat srcmat = imread(strpath, 0);
	//Mat srcmat(cvLoadImage(strpath.c_str(), 0));
	Mat ermat;
	threshold(srcmat, ermat, 147, 255, CV_THRESH_BINARY);
	HProjection(ermat);
	VProjection(ermat);
	XProjection(ermat);
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yokon_D

您的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值