我想在 photoshop
中混合两个图像,例如 multiply blending
,我想在我的应用程序中使用 C++ 在 OpenCv 中做同样的事情,我访问了 this很多次,每次都试图理解,但我没有得到它,我搜索了很多但没有得到我想要的东西 this但这有点奇怪,因为从 IplImages 到 ibl 等的转换很多,需要任何帮助、指南、想法和与 opencv 相关的示例。我经历了 Addweight
但我认为它与 Multiply Blending
我看到的公式here
目标 * 混合
下面是我尝试过的
Mat img1 = imread("E:\\img.jpg");
Mat img2 = Mat (img1.size(),img1.type());
vector<Mat> colors_1;
split(img2, colors_1);
colors_1[0] = 113;
colors_1[1] = 221;
colors_1[2] = 216;
merge(colors_1,img2);
Mat result(img1.size(), CV_32F);
for(int i = 0; i < img1.size().height; ++i){
for(int j = 0; j < img1.size().width; ++j){
for (int rgb=0 ; rgb<=img1.channels();rgb++){
float target = float(img1.at<uchar>(i, j)) / 255;
float blend = float(img2.at<uchar>(i, j)) / 255;
result.at<float>(i, j) = target*blend;
}
}
}
结果是 GrayScale 并且看起来不准确
谢谢
最佳答案
您没有正确访问图像 channel 。此外,您不需要将结果存储在 float
中。图片,uchar
可以。此外,您在 RGB channel 上的循环应该在 rgb<img1.channels()
时结束。 .
试试这段代码:
cv::Mat img1 = cv::imread("E:\\img.jpg");
cv::Mat img2 = cv::Mat (img1.size(),img1.type());
std::vector<cv::Mat> colors_1;
cv::split(img2, colors_1);
colors_1[0] = 113;
colors_1[1] = 221;
colors_1[2] = 216;
cv::merge(colors_1,img2);
cv::Mat result(img1.size(), CV_8UC3);
for(int i = 0; i < img1.rows; ++i){
for(int j = 0; j < img1.cols; ++j){
for (int c=0 ; c<img1.channels();c++){
uchar target = img1.at<uchar>(i, 3*j+c);
uchar blend = img2.at<uchar>(i, 3*j+c);
result.at<uchar>(i, 3*j+c) = cv::saturate_cast<uchar>(target*blend/255.);
}
}
}
关于c++ - 如何在 opencv 中进行 Mul 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267919/