opencv2等比缩放图片

本代码缩放图片后图片清晰的保持不变

前提必须有opencv_world343的dll和lib库,版本随意选,一般安装的opencv软件只有64位的库,这是最操蛋的地方

#include <WINDOWS.H>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2//highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <direct.h>
#include <io.h>
using namespace std;
#pragma  comment(lib, "opencv_world343.lib")

//图片等比压缩
cv::Mat PicRatioCompre(cv::Mat &srcImage, float fWidth, float fHeight)
{
	//获取输出图像分辨率
	int nRows = cvRound(srcImage.rows * fWidth);
	int nCols = cvRound(srcImage.cols * fHeight);
	cv::Mat resultImage(nRows, nCols, srcImage.type());
	for (int i = 0; i < nRows; i++)
	{
		for (int j = 0; j < nCols; j++)
		{
			//根据水平因子计算坐标,static_cast用于类型转换
			int x = static_cast<int>((i + 1) / fWidth + 0.5) - 1;
			//根据垂直因子计算坐标
			int y = static_cast<int>((j + 1) / fHeight + 0.5) - 1;
			resultImage.at<cv::Vec3b>(i, j) = srcImage.at<cv::Vec3b>(x, y);
		}
	}
	return resultImage;
}

//加载所有图片用于等比压缩
void main()
{
	intptr_t hFile = 0;
	struct _finddata_t fileinfo;
	vector<string> imagesVector;
	vector<string> imagesNewVector;
	string p;
	std::string strPath;
	std::string strNewPath;
	strPath = "C:/Users/User/Desktop/标注工具纸筒侧面测试数据";
	strNewPath = strPath + "_new";
	bool flag = CreateDirectory(strNewPath.c_str(), NULL);
	strPath += "/";
	strNewPath += "/";
	//获取所有jpg图片名
	hFile = _findfirst(p.assign(strPath).append("/*.jpg").c_str(), &fileinfo);

	if (hFile != -1) {
		do {
			imagesVector.push_back(strPath + fileinfo.name);//保存类名
			imagesNewVector.push_back(strNewPath + fileinfo.name);//保存类名
		} while (_findnext(hFile, &fileinfo) == 0);
	}

	//获取所有bmp图片名
	hFile = _findfirst(p.assign(strPath).append("/*.bmp").c_str(), &fileinfo);

	if (hFile != -1) {
		do {
			imagesVector.push_back(strPath + fileinfo.name);//保存类名
			imagesNewVector.push_back(strNewPath + fileinfo.name);//保存类名
		} while (_findnext(hFile, &fileinfo) == 0);
	}
	
	vector<std::string>::iterator iter = imagesVector.begin();
	vector<std::string>::iterator iterNew = imagesNewVector.begin();
	for (; iter != imagesVector.end(); iter++)
	{
		cv::Mat srcImage = cv::imread(*iter);
		if (srcImage.empty())
		{
			return;
		}
		//cv::imshow("原图像", srcImage);显示图像
		cv::Mat resultImage1 = PicRatioCompre(srcImage, 0.2, 0.2);//压缩为原图的五分之一
		FILE * rgba_file = fopen((*iterNew).c_str(), "wb");
		cv::imwrite(*iterNew, resultImage1);
		iterNew++;
	}

	
	return;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值