You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OpenCV利用掩码校正图像局部区域的白平衡偏差?

白平衡校正实现方案求助

我有一张室内场景图像,画面中包含一扇通往另一房间的门。拍摄所在房间的白平衡表现正常,但另一房间因光照条件不同出现白平衡偏差。我希望通过掩码将原图像中白平衡正确的白色像素映射至另一房间的对应像素(已获取两类区域的掩码)。

以下是我当前的实现代码:

cv::Mat blueMask;
cv::Mat whiteMask;
cv::Mat hsv;
getWhiteColorMask(src, blueMask, whiteMask, hsv);

// Save the HSV image
bool success = cv::imwrite(outputImagePath + "hsv.jpg", hsv);
// Save the white mask image
success = cv::imwrite(outputImagePath + "white_mask.jpg", whiteMask);
success = cv::imwrite(outputImagePath + "blue_mask.jpg", blueMask);
// Apply changes to the blue channel based on the mask
cv::Mat adjustedImage = src.clone();  // Clone the original image for adjustment

for (int row = 0; row < src.rows; ++row) {
    for (int col = 0; col < src.cols; ++col) {
        // Check if the pixel is in the blue mask
        if (blueMask.at<uchar>(row, col) > 0) {
            // Get the corresponding pixel in the white mask
            cv::Vec3b whitePixel = whiteMask.at<cv::Vec3b>(row, col);
        
            // Adjust the pixel in the blue mask using the white color
            cv::Vec3b& bluePixel = adjustedImage.at<cv::Vec3b>(row, col);
            bluePixel[0] = whitePixel[0];  // B channel
            //bluePixel[1] = whitePixel[1];  // G channel
            //bluePixel[2] = whitePixel[2];  // R channel
        }
    }
}
// Save the modified image with the new file name
std::string newImagePath = "./mask_test/white_masks/adjustedImage.jpg";
success = cv::imwrite(newImagePath, adjustedImage);

为简化代码已移除所有验证打印语句,无需关注bool类型的success变量。相关素材图像如下:

  • 源图像:源图像
  • 白平衡偏差区域掩码:白平衡偏差区域掩码
  • 白平衡正常区域掩码:白平衡正常区域掩码
  • HSV图像:HSV图像

现寻求针对该需求的正确实现方案或代码优化建议。

内容的提问来源于stack exchange,提问作者Lucy Janssens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 23:05:31