#include <iostream>
#include <fstream>
#include <windows.h>
#include "opencv2/opencv.hpp"
#pragma comment(lib, "opencv_world470.lib")
int convertToPowerOfTwo(int number) {
if ((number & (number - 1)) == 0) {
return number;
}
int convertedValue = 1;
while (convertedValue < number) {
convertedValue <<= 1;
}
return convertedValue;
}
cv::Mat cropImage(cv::Mat image) {
cv::Mat grayscaleImage;
cv::cvtColor(image, grayscaleImage, cv::COLOR_BGR2GRAY);
cv::Mat edgeImage;
cv::Canny(grayscaleImage, edgeImage, 50, 150);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(edgeImage, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
double maxContourArea = 0;
int maxValueIndex = -1;
for (int i = 0; i < contours.size(); i++) {
double contourArea = cv::contourArea(contours[i]);
if (contourArea > maxContourArea) {
maxContourArea = contourArea;
maxValueIndex = i;
}
}
cv::Rect boundingBox = cv::boundingRect(contours[maxValueIndex]);
cv::Mat croppedImage = image(boundingBox);
return croppedImage;
}
int resizeImage(std::string imagePath, std::string originalImageFormat, std::string targetImageFormat) {
cv::Mat image = cv::imread(imagePath + originalImageFormat);
if (image.empty()) {
std::cout << "Failed to load image!" << std::endl;
return -1;
}
int width = convertToPowerOfTwo(image.cols);
int height = convertToPowerOfTwo(image.rows);
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(width, height));
if (resizedImage.empty()) {
std::cout << "Failed to resize image!" << std::endl;
return -1;
}
cv::imwrite(imagePath + targetImageFormat, resizedImage);
return 0;
}
int main() {
resizeImage("1", ".jpg", ".bmp");
return 0;
}
08-20
3677
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-03
4089
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)