天天看点

等比例缩放图片-opencv数据结构定义等比例缩放函数效果show me the code 完整代码

数据结构定义

如果dw 和 dh 有值,则缩放成为dw或者dh 为最终值的图片,fix_type中msrc为源图,mdest为需要生成的图
typedef enum fix_enum
{
   enum_x_fix,
   enum_y_fix,
   enum_xy_fix
}fix_enum;

typedef struct fix_type
{
	//int sw;
	//int sh;
	int dw;
	int dh;
	fix_enum fix;
//	string dest;
	Mat msrc;
	Mat mdest;
}fix_type;
           

等比例缩放函数

算法如下:
//1 指定x宽度,y成比例
//2 制定y高度,x成比例

String get_dest(String &srcImage,fix_type &ftype) {
	
	size_t t = srcImage.find_last_of(".");
	String destfile = srcImage.substr(0, t);
	String after = srcImage.substr(t, srcImage.length() - t);

	double scale = 1.0f;
	int width  = ftype.msrc.cols;
	int height = ftype.msrc.rows;

	int ddw = 0;
	int ddh = 0;
	switch (ftype.fix) 
	{
	case enum_x_fix: //width 宽度,y成比例
		scale = (float)ftype.dw / (float)width ;
		ddw = width*scale;
		ddh = height*scale;
		break;
	case enum_y_fix:
		scale = (double)ftype.dh / (double)height;
		ddw = width *scale;
		ddh = width *scale;
		break;
	case enum_xy_fix:
		ddw = ftype.dw;
		ddh = ftype.dh;
		break;
	}
	stringstream s;
	s<< destfile;
	s << "_thumb_";
	s << ddw << "_" << ddh << after;
	//destfile = destfile+  "_thumb_"+ itoa(ddw + ddh + after;

	//Size S = Size(width*scale, height*scale);
	Size S = Size(ddw, ddh);
	cv::resize(ftype.msrc, ftype.mdest, S, 0, 0);

	//cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
	//imshow("test", ftype.mdest);
	//cvWaitKey(0);
	return s.str();
}
           

效果

缩小

填入参数

等比例缩放图片-opencv数据结构定义等比例缩放函数效果show me the code 完整代码
等比例缩放图片-opencv数据结构定义等比例缩放函数效果show me the code 完整代码

放大

等比例缩放图片-opencv数据结构定义等比例缩放函数效果show me the code 完整代码

show me the code 完整代码

#define _CRT_SECURE_NO_DEPRECATE
#include <opencv2\opencv.hpp>
#include <stdio.h>
//#include <time.h>
#include <string>
//using namespace tinyxml2;
using namespace cv;
using namespace std;

#ifndef _DEBUG
#pragma comment(lib,"opencv_world440.lib")
#else
#pragma comment(lib,"opencv_world440d.lib")
#endif
#define MAX_PATH 260

typedef enum fix_enum
{
   enum_x_fix,
   enum_y_fix,
   enum_xy_fix
}fix_enum;

typedef struct fix_type
{
	//int sw;
	//int sh;
	int dw;
	int dh;
	fix_enum fix;
//	string dest;
	Mat msrc;
	Mat mdest;
}fix_type;

const String keys =
"{ help  | false | print usage         }"
"{ image   | dog.jpg | path to image file }"
"{ width   | 0   | image to width  }"
"{ height  | 0   | image to height }"
;


//1 指定x宽度,y成比例
//2 制定y高度,x成比例

String get_dest(String &srcImage,fix_type &ftype) {
	
	size_t t = srcImage.find_last_of(".");
	String destfile = srcImage.substr(0, t);
	String after = srcImage.substr(t, srcImage.length() - t);

	double scale = 1.0f;
	int width  = ftype.msrc.cols;
	int height = ftype.msrc.rows;

	int ddw = 0;
	int ddh = 0;
	switch (ftype.fix) 
	{
	case enum_x_fix: //width 宽度,y成比例
		scale = (float)ftype.dw / (float)width ;
		ddw = width*scale;
		ddh = height*scale;
		break;
	case enum_y_fix:
		scale = (double)ftype.dh / (double)height;
		ddw = width *scale;
		ddh = width *scale;
		break;
	case enum_xy_fix:
		ddw = ftype.dw;
		ddh = ftype.dh;
		break;
	}
	stringstream s;
	s<< destfile;
	s << "_thumb_";
	s << ddw << "_" << ddh << after;
	//destfile = destfile+  "_thumb_"+ itoa(ddw + ddh + after;

	//Size S = Size(width*scale, height*scale);
	Size S = Size(ddw, ddh);
	cv::resize(ftype.msrc, ftype.mdest, S, 0, 0);

	//cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
	//imshow("test", ftype.mdest);
	//cvWaitKey(0);
	return s.str();
}



int main(int argc, char** argv) {

	CommandLineParser parser(argc, argv, keys);
	String srcImage = parser.get<String>("image");
	string type = "";
	string dest;
	
	fix_type fixtype;
	fixtype.fix = enum_x_fix;
	fixtype.msrc = imread(srcImage);

	if (fixtype.msrc.empty())
	{
		cout << "{\"ret\":-1,\"url\":\"null\"}";
		return -1;
	}

	fixtype.dw = parser.get<int>("width");
	fixtype.dh = parser.get<int>("height");
	int dw = fixtype.dw;
	int dh = fixtype.dh;
	//cout << "dw is " << dw << " " << "dh is " << dh << endl;
	if ((dw > 0) && (dh == 0))
		fixtype.fix = enum_x_fix;
	else if ((dw == 0) && (dh > 0))
		fixtype.fix = enum_y_fix;
	else if ((dw > 0) && (dh > 0))
		fixtype.fix = enum_xy_fix;
	else {
		fixtype.dw = 100;
		fixtype.dh = 0;
		fixtype.fix = enum_x_fix;
	}
	

	//std::cout << fixtype.msrc.rows << " " << fixtype.msrc.cols << endl;
	String file = get_dest(srcImage,fixtype);
	imwrite(file, fixtype.mdest);
	imshow("ori", fixtype.msrc);
	imshow("tra", fixtype.mdest);
	cv::waitKey(0);
	cout << "{\"ret\":1,\"url\":\"" + file + "\"}";
	return 0;
	}
           

继续阅读