项目【MFC】总结(三)——图像灰度化/彩色化

关于界面左边的按钮事件 “上一帧”、“下一帧”、“跳转至”以及三个“编辑框显示”的功能比较简单,这里就不在记录了。需要注意的是,变量命名一定要规范,这样自己就不容易混乱。比如,原始读入的图像为Img,当前帧为Current_Img,调整帧号为Goto_farme_num等。

这一次记录一下右边的【彩色化】和【灰度化】功能,可能更加偏向图像方面而非MFC了。

灰度化 /彩色化

很简单,灰度化就是将读入的图像直接显示,这里涉及到数组转图像(Vector->Mat)。

数组转图像代码如下:

//数组转Mat
void VecToMat(vector<vector<int>> Vec, Mat &img){
	int rows = Vec.size();
	int cols = Vec[0].size();
	short *ptmp = NULL;
	img = cv::Mat::zeros(Size(cols, rows), CV_16UC1);
	for (int i = 0; i <rows; ++i)
	{
		ptmp = img.ptr<short>(i);

		for (int j = 0; j < cols; ++j)
		{
			if (Vec[i][j] < 0)
				ptmp[j] = 0;
			else
				ptmp[j] = Vec[i][j];

		}
	}
}

 

 灰度化就直接将上述转好的图像显示即可,彩色化需要将灰度图转为伪彩色图,主要用到OpenCV的API:applyColorMap

代码如下:

cv::Mat get_img(vector<vector<vector<int>>> csvData_Slip, int res_X, int res_Y,int num,bool color){
	int rows = csvData_Slip[0].size();
	int cols = csvData_Slip[0][0].size();
	Mat img = cv::Mat::zeros(Size(cols, rows), CV_16UC1);
	for (int i = num; i <= num; ++i){
		VecToMat(csvData_Slip[i], img);
		img.convertTo(img, CV_8U, 1, 0);
		if (color)
		cv::applyColorMap(img, img, 2);//2、6  伪彩色图
	}
	return img;

}

applyColorMap参数详解:https://blog.csdn.net/loveliuzz/article/details/73648505

 

整个灰度化/彩色化代码,不可直接复制(有很多全局变量未添加):

//灰度
void CTouch_test_CKV10Dlg::OnBnClickedButtongray()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	zoom = false;
	if (Frame_Num > 0){
		Color = false;
		img = get_img(csvData_Slip, 1920, 1080, current_frame - 1, Color);
		CRect rect;
		GetDlgItem(IDC_STATIC_IMG)->GetClientRect(&rect);
		Rect dst(rect.left, rect.top, rect.right, rect.bottom);
		resize(img, current_img, cv::Size(rect.Width(), rect.Height()));
		//若前一帧缩放,后一帧也保持前一张的尺寸不变
		if (pre_zoom){
			double  new_zoom = double(resize_mat.rows) / current_img.rows;
			resize(current_img, current_img, Size(), new_zoom, new_zoom, INTER_NEAREST); //按比例放大图片
		}
		//分辨率
		RX = current_img.cols;
		RY = current_img.rows;
		if (g_iZoom <= 1)
			m_resize = g_iZoom * 4;
		else
			m_resize = g_iZoom + 3;
		imshow("view", current_img);
		UpdateData(FALSE);
	}
	else{
		INT_PTR nRes;
		// 显示消息对话框   
		nRes = MessageBox(_T("请先读入数据!"), _T("数据读取"), MB_OKCANCEL | MB_ICONWARNING);
	}
}

//彩色
void CTouch_test_CKV10Dlg::OnBnClickedButtoncolor()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	zoom = false;
	if (Frame_Num > 0){
		Color = true;
		img = get_img(csvData_Slip, 1920, 1080, current_frame - 1, Color);
		CRect rect;
		GetDlgItem(IDC_STATIC_IMG)->GetClientRect(&rect);
		Rect dst(rect.left, rect.top, rect.right, rect.bottom);
		resize(img, current_img, cv::Size(rect.Width(), rect.Height()));
		
		//若前一帧缩放,后一帧也保持前一张的尺寸不变
		if (pre_zoom){
			double  new_zoom = double(resize_mat.rows) / current_img.rows;
			resize(current_img, current_img, Size(), new_zoom, new_zoom, INTER_NEAREST); //按比例放大图片
		}
		//分辨率
		RX = current_img.cols;
		RY = current_img.rows;
		if (g_iZoom <= 1)
			m_resize = g_iZoom * 4;
		else
			m_resize = g_iZoom + 3;

		imshow("view", current_img);
		UpdateData(FALSE);
	}
	else{
		INT_PTR nRes;
		// 显示消息对话框   
		nRes = MessageBox(_T("请先读入数据!"), _T("数据读取"), MB_OKCANCEL | MB_ICONWARNING);
	}
}

END!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值