#include <opencv2/opencv.hpp>
#include <iostream>
// 剪裁并调整图像大小的函数
// 参数:inputImage - 输入的图像;left - 左边剪裁的像素数量;top - 上边剪裁的像素数量;
// right - 右边剪裁的像素数量;bottom - 下边剪裁的像素数量
// 返回值:剪裁并调整大小后的图像
cv::Mat cropAndResize(const cv::Mat& inputImage, int left, int top, int right, int bottom) {
// 获取输入图像的行数和列数
int rows = inputImage.rows;
int cols = inputImage.cols;
// 检查剪裁边界是否合法
// 剪裁边界不能为负数,且左右剪裁的像素总和不能超过图像的列数,上下剪裁的像素总和不能超过图像的行数
if (left < 0 || top < 0 || right < 0 || bottom < 0 ||
left + right >= cols || top + bottom >= rows) {
// 若剪裁参数不合法,输出错误信息
std::cerr << "Invalid crop parameters!" << std::endl;
// 返回原始图像
return inputImage;
}
// 计算剪裁后图像的新宽度和新高度
int newWidth, newHeight;
newWidth = cols - left - right;
newHeight = rows - top - bottom;
// 定义剪裁区域
// cv::Rect 用于创建一个矩形区域,参数分别为左上角点的 x 坐标、y 坐标、宽度和高度
cv::Rect roi(left, top, newWidth, newHeight);
// 剪裁图像
// 通过 cv::Mat 的构造函数,传入 roi 区域,从输入图像中提取出剪裁区域的图像
cv::Mat croppedImage = inputImage(roi);
// 调整剪裁后图像的大小
cv::Mat resizedImage;
// cv::resize 函数用于调整图像大小,参数分别为输入图像、输出图像和目标大小
cv::resize(croppedImage, resizedImage, cv::Size(newWidth, newHeight));
// 返回调整大小后的图像
return resizedImage;
}
// 去除图像外围指定像素值的像素,并记录四个边的剪裁数量
// 参数:inputImage - 输入的图像;targetValue - 要去除的目标像素值;maxToKeep - 保留的最大圈数;
// top_cut - 记录上边剪裁的像素数量;bottom_cut - 记录下边剪裁的像素数量;
// left_cut - 记录左边剪裁的像素数量;right_cut - 记录右边剪裁的像素数量
// 返回值:处理后的图像
cv::Mat removePeripheryPixels(const cv::Mat& inputImage, uchar targetValue, int maxToKeep, int& top_cut, int& bottom_cut, int& left_cut, int& right_cut) {
// 克隆输入图像,避免修改原始图像
cv::Mat outputImage = inputImage.clone();
// 获取输出图像的行数和列数
int rows = outputImage.rows;
int cols = outputImage.cols;
// 计算行数和列数的一半
int half_rows = rows / 2;
int half_cols = cols / 2;
// 初始化四个边的剪裁数量为 0
top_cut = 0;
bottom_cut = 0;
left_cut = 0;
right_cut = 0;
// 处理上边
for (int i = 0; i < half_rows; ++i) {
// 标记当前行的所有像素是否都为目标像素值
bool allTargetValue = true;
// 遍历当前行的所有列
for (int c = 0; c < cols; ++c) {
// 若当前像素不等于目标像素值
if (outputImage.at<uchar>(i, c) != targetValue) {
// 标记为 false
allTargetValue = false;
// 跳出内层循环
break;
}
}
// 若当前行的所有像素都为目标像素值
if (allTargetValue) {
// 上边剪裁数量加 1
top_cut++;
}
}
// 处理下边
for (int i = 0; i < half_rows; ++i) {
// 标记当前行的所有像素是否都为目标像素值
bool allTargetValue = true;
// 计算当前处理的行号,从下往上
int r = rows - 1 - i;
// 遍历当前行的所有列
for (int c = 0; c < cols; ++c) {
// 若当前像素不等于目标像素值
if (outputImage.at<uchar>(r, c) != targetValue) {
// 标记为 false
allTargetValue = false;
// 跳出内层循环
break;
}
}
// 若当前行的所有像素都为目标像素值
if (allTargetValue) {
// 下边剪裁数量加 1
bottom_cut++;
}
}
// 处理左边
for (int i = 0; i < half_cols; ++i) {
// 标记当前列的所有像素是否都为目标像素值
bool allTargetValue = true;
// 遍历当前列的所有行
for (int r = 0; r < rows; ++r) {
// 若当前像素不等于目标像素值
if (outputImage.at<uchar>(r, i) != targetValue) {
// 标记为 false
allTargetValue = false;
// 跳出内层循环
break;
}
}
// 若当前列的所有像素都为目标像素值
if (allTargetValue) {
// 左边剪裁数量加 1
left_cut++;
}
}
// 处理右边
for (int i = 0; i < half_cols; ++i) {
// 标记当前列的所有像素是否都为目标像素值
bool allTargetValue = true;
// 计算当前处理的列号,从右往左
int c = cols - 1 - i;
// 遍历当前列的所有行
for (int r = 0; r < rows; ++r) {
// 若当前像素不等于目标像素值
if (outputImage.at<uchar>(r, c) != targetValue) {
// 标记为 false
allTargetValue = false;
// 跳出内层循环
break;
}
}
// 若当前列的所有像素都为目标像素值
if (allTargetValue) {
// 右边剪裁数量加 1
right_cut++;
}
}
// 根据最大保留圈数调整四个边的剪裁数量
// 若剪裁数量大于最大保留圈数,则减去最大保留圈数,否则保持不变
top_cut = top_cut > maxToKeep ? top_cut - maxToKeep : top_cut;
bottom_cut = bottom_cut > maxToKeep ? bottom_cut - maxToKeep : bottom_cut;
left_cut = left_cut > maxToKeep ? left_cut - maxToKeep : left_cut;
right_cut = right_cut > maxToKeep ? right_cut - maxToKeep : right_cut;
// 返回处理后的图像
return outputImage;
}
int main() {
// 读取灰度图像
// cv::imread 函数用于读取图像,cv::IMREAD_GRAYSCALE 表示以灰度模式读取
cv::Mat image = cv::imread("/path/to/image", cv::IMREAD_GRAYSCALE);
// 检查图像是否读取成功
if (image.empty()) {
// 若图像读取失败,输出错误信息
std::cout << "Could not open or find the image" << std::endl;
// 返回 -1 表示程序异常退出
return -1;
}
// 指定要去除的像素值
uchar targetValue = 128;
// 指定保留的最大圈数
int maxCirclesToKeep = 10;
// 分别指定剪裁的四个边
int top = 100;
int bottom = 200;
int left = 300;
int right = 400;
// 调用 removePeripheryPixels 函数处理图像,并更新四个边的剪裁数量
removePeripheryPixels(image, targetValue, maxCirclesToKeep, top, bottom, left, right);
// 剪裁并调整图像大小
// 调用 cropAndResize 函数对图像进行剪裁和调整大小操作
cv::Mat result = cropAndResize(image, left, top, right, bottom);
// 显示原始图像和处理后的图像
// cv::imshow 函数用于显示图像,第一个参数为窗口名称,第二个参数为要显示的图像
cv::imshow("Original Image", image);
cv::imshow("Processed Image", result);
// 等待用户按键
cv::waitKey(0);
// 返回 0 表示程序正常退出
return 0;
}
09-23
1096

01-28
6万+
