split () function + merge () function

In image processing, we are exposed to the RGB color mostly in order to analyze the characteristic of the image on a channel, the color channels of the image needs to be isolated, or re-fusion treatment after a color channel. Providing opencv split () function to separate color channels, there is provided merge () function to fusion color channels.

1.split () function

The effect of this function is to image a separate channel.

split () function definition:

void split(const Mat& m, vector<mat>& mv );</mat>

Parameter Description:

  • const Mat & type of src, we need to fill separate images;
  • vector <mat style = "margin-top: 0px;"> Music Videos type, filling the output array, or a function of the output vector of the container, i.e. the separated image; </ mat>

2.merge () function

() Function is a function of merge split () function in reverse operation, a plurality of arrays combined into a multi-channel array.

merge () function is defined:

void merge(const vector<mat>& mv, OutputArray dst );</mat>

  • const <mat style = "margin-top: 0px;"> mv type, filling the array to be merged vector vessel, mv this parameter must all matrix has the same size and depth; </ mat>
  • Save the image after the merger;

3. The use of split () function and the seal removed thresholding, merge the ticket () function merged image

There will always be some red stamp on the bill some important information area to cover, such as some personnel when invoicing seal cover more casual, easy, right to shelter some of the key areas, which makes it difficult to identify the next bill, so we bills must first be certain image pre-processing to remove the seal interference, then character recognition, recognition accuracy like this can only be guaranteed.


  • Task: We need to identify the invoice font (red stamp identifying affect all our work)

 

code show as below:

#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <iostream>
using namespace cv;
using namespace std;

int main() {
	//【1】定义相关变量
	Mat srcImage, newImage;
	Mat srcImage_B, srcImage_G, srcImage_R;
	//【2】存放Mat的数组vector
	vector<Mat> channels_BGR;
	//【3】读取原始图像并检查图像是否读取成功    
	srcImage = imread("E:\\VS2015Opencv\\vs2015\\project\\picture\\02.jpg");
	if (srcImage.empty())
	{
		cout << "读取图像有误,请重新输入正确路径!\n";
		getchar();
		return -1;
	}
	imshow("src原图像", srcImage);  //在窗口显示原图像
								 //【4】对加载的原图像进行通道分离,即把一个3通道图像转换成为3个单通道图像  
	split(srcImage, channels_BGR);
	//从数组中取出3种颜色,0通道为B分量,1通道为G分量,2通道为R分量。因为:RGB色彩空间在opencv中默认通道顺序为BGR!!!
	srcImage_B = channels_BGR.at(0);
	srcImage_G = channels_BGR.at(1);
	srcImage_R = channels_BGR.at(2);
	imshow("srcImage_B通道", srcImage_B); //分别显示R,G,B各个通道图像  
	imshow("srcImage_G通道", srcImage_G);
	imshow("srcImage_R通道", srcImage_R);

	//【5】 全局二值化
	Mat gray;
	cvtColor(srcImage, gray, CV_RGB2GRAY);
	int th = 170; //阈值要根据实际情况调整
	Mat binary;
	//CV_THRESH_BINARY代表阈值其中一种模式,
	threshold(gray, binary, th, 255, CV_THRESH_BINARY);
	Mat red_binary;
	threshold(srcImage_R, red_binary, th, 255, CV_THRESH_BINARY);
	imshow("灰色图 + 阈值处理 ", binary);
	imshow("R通道+阈值处理", red_binary);
	//【6】将3个单通道重新合并成一个三通道图像  
	merge(channels_BGR, newImage);
	imshow("将R,G,B通道合并后的图像", newImage);
	waitKey(0);
	return 0;

}

  上述博文参考:https://www.jianshu.com/p/dcc9c29f10b4

 

Guess you like

Origin www.cnblogs.com/fcfc940503/p/11246690.html