添加图像缩放功能

This commit is contained in:
zjc-zjc-123 2024-11-08 12:24:07 +08:00
parent 2590c25e2c
commit cd144620e3

View File

@ -5,32 +5,35 @@
#include <windows.h>
#include <commdlg.h> // 包含文件对话框相关的函数
using namespace cv;
using namespace std;
/**
* @brief
*
* BGR HSV (S)
*
* @brief 绿 Lab 绿
*
* @param inputImage cv::Mat BGR
* @param outputImage cv::Mat
* @param saturationThreshold int 0 255
*
*
* @note
*
* @param outputImage cv::Mat绿
* @param params 绿
*/
/**
* @brief
*
* @param inputImage cv::Mat BGR
* @param outputImage cv::Mat
* @param params
*/
void vibrantGreenDetection(const Mat& inputImage, Mat& outputImage, const map<string, int>& params) {
// 从参数映射中获取绿色阈值
int green = params.at("green");
// 将输入图像从 BGR 转换为 Lab
Mat lab_image;
cvtColor(inputImage, lab_image, cv::COLOR_BGR2Lab);
// 定义偏绿色的 Lab 范围(具体值可能需要调整)
Scalar lower_green_lab(101, 101, 95);
Scalar upper_green_lab(135, 120, green);
// 创建掩膜
Mat mask_lab;
inRange(lab_image, lower_green_lab, upper_green_lab, mask_lab);
// 通过掩膜提取偏绿色部分,将结果存储在 outputImage 中
bitwise_and(inputImage, inputImage, outputImage, mask_lab);
}
void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map<string, int>& params) {
// 从参数映射中获取饱和度阈值
int saturationThreshold = params.at("saturationThreshold");
@ -52,13 +55,6 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map<st
// 对饱和度图像应用阈值处理
threshold(saturation, outputImage, saturationThreshold, 255, THRESH_BINARY);
}
void blackColorDetection(const Mat& inputImage, Mat& outputImage, const map<string, int>& params)
{
outputImage = Mat::zeros(inputImage.size(), CV_8UC1);
}
string openFileDialog() {
// 初始化文件选择对话框
OPENFILENAME ofn; // 文件对话框结构
@ -89,8 +85,6 @@ string openFileDialog() {
return ""; // 如果用户取消,返回空字符串
}
void test() {}
Mat readImage() {
// 读取输入图像
string imagePath = openFileDialog();
@ -111,10 +105,22 @@ Mat readImage() {
return image;
}
// 辅助函数,用于调整图像大小并显示,支持等比例放大
void showImage(const string& windowName, const Mat& img, double scaleFactor = 1.0) {
Mat resizedImg;
int newWidth = static_cast<int>(img.cols * scaleFactor);
int newHeight = static_cast<int>(img.rows * scaleFactor);
// 调整图像大小
resize(img, resizedImg, Size(newWidth, newHeight));
// 显示图像
imshow(windowName, resizedImg);
}
int main() {
// 读取输入图像
Mat inputImage = readImage();
Mat inputImage = readImage();
if (inputImage.empty()) {
cout << "Error: Could not load image." << endl;
@ -126,16 +132,19 @@ int main() {
// 使用 map 模拟 JSON 参数传递
map<string, int> params;
params["saturationThreshold"] = 100; // 设置饱和度阈值为100
params["green"] = 134; // 设置绿色阈值
// 调用鲜艳色检测函数
vibrantColorDetection(inputImage, outputImage, params);
// 调用鲜艳绿色检测函数
vibrantGreenDetection(inputImage, outputImage, params);
// 显示原图和检测到的鲜艳区域
imshow("Original Image", inputImage);
imshow("Detected Vibrant Colors", outputImage);
// 定义缩放因子1.0 表示原始大小,>1.0 表示放大,<1.0 表示缩小
double scaleFactor = 1.5; // 将图像放大1.5倍
// 显示原图和检测到的绿色区域,使用缩放因子
showImage("Original Image", inputImage, scaleFactor);
showImage("Detected Vibrant Green", outputImage, scaleFactor);
// 等待用户按键
waitKey(0);
return 0;
}
}