OpenCV图像处理常用接口封装

本文详细介绍了OpenCV库中常用的图像处理接口,包括图像读取、转换、滤波、色彩空间操作等,并展示了如何进行封装以提高代码复用性。通过实例代码,读者可以了解如何高效地利用OpenCV进行图像处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctime>
#include <math.h> 
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

#include "VCAString.h"
#include "VCAFileAccess.h"
#include "VCAImage.h"
#include "VCAMath.h"
#include "VCALogManage.h"

Scalar g_red(0, 0, 255);
Scalar g_cyan(255, 255, 0);
Scalar g_gray(200, 200, 200);
Scalar g_white(255, 255, 255);
Scalar g_black(0, 0, 0);
Scalar g_green(0, 255, 0);
Scalar g_yellow(0, 255, 255);
Scalar g_pinkish_red(255, 0, 255);
Scalar g_blue(255, 0, 0);


bool SaveAImage(const char *p_file_name, const Mat &input_image, unsigned long max_file_size)
{
	VcaCreateFilePath(p_file_name);
	if(max_file_size==0)
	{
		imwrite(p_file_name, input_image);
		return true;
	}
	else
	{
		int string_len=strlen(p_file_name);
		vector<uchar> buff;
		vector<int> param=vector<int>(2);
		int buff_size;
		if((VCA_STRICMP(p_file_name+string_len-4, ".jpg")==0)||(VCA_STRICMP(p_file_name+string_len-5, ".jpeg")==0))
		{
			
			param[0]=CV_IMWRITE_JPEG_QUALITY;
			for(param[1]=95;param[1]>0;param[1]-=10)
			{
				buff.clear();
				imencode(".jpg", input_image, buff, param);
				buff_size=buff.size();
				if(buff_size<=max_file_size)
				{
					//imwrite(p_file_name, input_image, param);
					SaveBinData(p_file_name, &buff[0], buff_size);
					return true;
				}
			}
		}
		else if(VCA_STRICMP(p_file_name+string_len-4, ".png")==0)
		{
			param[0]=CV_IMWRITE_PNG_COMPRESSION;
			for(param[1]=0;param[1]<=9;param[1]++)
			{
				buff.clear();
				imencode(".png", input_image, buff, param);
				buff_size=buff.size();
				if(buff_size<=max_file_size)
				{
					SaveBinData(p_file_name, &buff[0], buff_size);
					return true;
				}
			}
		}
	}
	return false;
}

bool LoadAImage(const char *p_file_name, Mat &output_image)
{
	if(p_file_name==NULL)
	{
		return false;
	}
	if(strlen(p_file_name)==0)
	{
		return false;
	}
	if(!VcaFileExist(p_file_name))
	{
		return false;
	}
	output_image=imread(p_file_name);
	if(output_image.empty())
	{
		return false;
	}
	return true;
}

bool CreateAImage(Mat &o_image, Size image_size, int channel_num)
{
	if(channel_num==1)
	{
		o_image.create(image_size, CV_8UC1);
	}
	else if(channel_num==3)
	{
		o_image.create(image_size, CV_8UC3);
	}
	else
	{
		return false;
	}
	if(o_image.empty())
	{
		return false;
	}
	return true;
}


void ShowTagColor(Mat &input_image, Point *p_pt, const char *p_msg_buffer, Scalar color, bool show_tag_bg)
{
	Rect msg_rect;

	msg_rect.width=(int)((float)strlen(p_msg_buffer)*(float)5.2)+3;
	msg_rect.height=13;
	msg_rect.x=p_pt->x;
	msg_rect.y=p_pt->y;
	if(msg_rect.x<0)
	{
		msg_rect.x=0;
	}
	if(msg_rect.y<0)
	{
		msg_rect.y=0;
	}
	if((msg_rect.x+msg_rect.width)>input_image.cols)
	{
		msg_rect.x=input_image.cols-msg_rect.width;
	}
	if((msg_rect.y+msg_rect.height)>input_image.rows)
	{
		msg_rect.y=input_image.rows-msg_rect.height;
	}
	if(show_tag_bg)
	{
		rectangle(input_image,msg_rect,color,-1, CV_AA);
		putText(input_image, p_msg_buffer, cvPoint(msg_rect.x,msg_rect.y+9),CV_FONT_HERSHEY_SIMPLEX,(double)0.3,Scalar(0,0,0),1,CV_AA);
	}
	else
	{
		putText(input_image, p_msg_buffer, cvPoint(msg_rect.x,msg_rect.y+9),CV_FONT_HERSHEY_SIMPLEX,(double)0.3,color,1,CV_AA);
	}
}


void ShowTagRMCT(Mat &input_image, Rect &o_rect, const char *p_msg_buffer, Scalar o_scalar, int thickness, bool show_position, bool show_tag_bg, int tag_position)
{
	char msg_buffer[256];
	Point tag_pt;
	int offset_length=0;

	memset(msg_buffer, 0, sizeof(msg_buffer));
	if(p_msg_buffer)
	{
		offset_length+=VCA_SPRINTF_S(msg_buffer, sizeof(msg_buffer)-offset_length, "%s", p_msg_buffer);
	}
	if(show_position)
	{
		offset_length+=VCA_SPRINTF_S(&msg_buffer[offset_length], sizeof(msg_buffer)-offset_length, "%s(%u,%u,%u,%u)", p_msg_buffer, o_rect.x, o_rect.y, o_rect.width, o_rect.height);
	}
	if(offset_length>0)
	{
		offset_length+=VCA_SPRINTF_S(&msg_buffer[offset_length], sizeof(msg_buffer)-offset_length, "  ");
	}

	tag_pt.x=o_rect.x;
	if(tag_position==0)
	{
		tag_pt.y=o_rect.y-13;
	}
	else if(tag_position==1)
	{
		tag_pt.y=o_rect.y;
	}
	else if(tag_position==2)
	{
		tag_pt.y=o_rect.y+o_rect.height-13;
	}
	else if(tag_position==3)
	{
		tag_pt.y=o_rect.y+o_rect.height;
	}
	rectangle(input_image, o_rect, o_scalar, thickness, CV_AA);
	ShowTagColor(input_image, &tag_pt, msg_buffer, o_scalar, show_tag_bg);
}

void ShowLine(Mat &input_image, Rect &o_rect, Scalar o_scalar, int thickness)
{
	line(input_image, Point(o_rect.x, o_rect.y),	Point(o_rect.width, o_rect.height), o_scalar, thickness, CV_AA);
}

void ShowLine(Mat &input_image, Point &pt1, Point &pt2, Scalar o_scalar, int thickness)
{
	line(input_image, pt1, pt2, o_scalar, thickness, CV_AA);
}

void ShowPoint(Mat &input_image, Point &o_point, Scalar o_scalar, int size)
{
	circle(input_image, o_point, size, o_scalar, -1, CV_AA);
}

void ShowTagPoint(Mat &input_image, Point &o_point, Scalar o_scalar)
{
	char msg_buffer[32];
	Point tag_pt;

	ShowPoint(input_image, o_point, o_scalar, 3);
	VCA_SPRINTF_S(msg_buffer, sizeof(msg_buffer), "%u,%u ", o_point.x, o_point.y);
	tag_pt.x=o_point.x+8;
	tag_pt.y=o_point.y;
	ShowTagColor(input_image, &tag_pt, msg_buffer, o_scalar);
}





void ShowText(Mat &input_image, P
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值