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

几何投影:对一幅简单二值图像的水平、垂直及对角线的投影。
(注:代码只适用于一些相对简单的二值图像,程序运行后生成的结果在文件夹里,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;
}
posted @   yokon  阅读(26)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示