|
发表于 2024-11-19 15:41:02
|
显示全部楼层
对于黑白二值图像,不需要特别设置颜色顺序,因为每个像素点只包含单一的灰度值。在这种情况下,转换过程中可以直接使用灰度值进行设置。
下面是调整后的代码,以处理黑白二值图像的方式进行转换和拼接:
- #include <opencv2/opencv.hpp>
- #include <atlimage.h>
- void StitchImageUsingMFC(const std::string& imagePath, const std::string& outputPath, int blockHeight)
- {
- // 使用OpenCV加载大图像
- cv::Mat originalMat = cv::imread(imagePath, cv::IMREAD_GRAYSCALE);
- if (originalMat.empty())
- {
- AfxMessageBox(_T("图像加载失败!"));
- return;
- }
- int imageWidth = originalMat.cols;
- int imageHeight = originalMat.rows;
- // 创建一个空的目标图像
- CImage targetImage;
- targetImage.Create(imageWidth, imageHeight, 8);
- int currentHeight = 0;
- while (currentHeight < imageHeight)
- {
- int currentBlockHeight = std::min(blockHeight, imageHeight - currentHeight);
- // 从Mat中提取一个块
- cv::Rect roi(0, currentHeight, imageWidth, currentBlockHeight);
- cv::Mat blockMat = originalMat(roi);
- // 转换Mat为CImage
- CImage blockImage;
- blockImage.Create(blockMat.cols, blockMat.rows, 8);
- for (int y = 0; y < blockMat.rows; ++y)
- {
- for (int x = 0; x < blockMat.cols; ++x)
- {
- uchar grayValue = blockMat.at<uchar>(y, x);
- blockImage.SetPixel(x, y, RGB(grayValue, grayValue, grayValue));
- }
- }
- // 将blockImage复制到targetImage中的相应位置
- for (int y = 0; y < blockImage.GetHeight(); ++y)
- {
- for (int x = 0; x < blockImage.GetWidth(); ++x)
- {
- COLORREF color = blockImage.GetPixel(x, y);
- targetImage.SetPixel(x, currentHeight + y, color);
- }
- }
- currentHeight += currentBlockHeight;
- }
- // 保存拼接后的图像
- targetImage.Save(_T(outputPath.c_str()));
- }
- int main()
- {
- StitchImageUsingMFC("path_to_your_image.jpg", "output_image.jpg", 100);
- return 0;
- }
复制代码
请确认将代码中的图像路径替换为当前的实际路径。对于CImage,请注意它可能需要一个调色板来处理8位灰度。如果遇到相关问题,可以考虑将目标图像设置为24位来避免复杂的调色板处理。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|