我想写一个C程序,改变图片的颜色,然后保存它。它还会对不止一种颜色分别进行处理。例如,我有一张红色、蓝色、紫色和绿色的图片。程序应更改所有颜色为黑色,除了红色和保存图片。在原图中,应将颜色更改为黑色除蓝色,并保存另一张图片。然后把紫色和绿色分开做。在保存文件之前,窗口会显示图片。
下面是我的代码:#include <iostream>
#include <iostream>
#include <iostream>
#include <vector>
#include<time.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(int argc, char **argv)
{
//Files' name and windows' name
char *resim[3] = {"112.png", "116.png", "113.png"};
//b,g,r values; 1-Min, 2-Max, 3-Wil be
int imgB1[] ={150, 190, 0};
int imgB2[] ={220 ,255, 80};
int imgB3[] ={168,190, 66};
int imgG1[] ={100,180, 90};
int imgG2[] ={150,255, 150};
int imgG3[] ={119, 200, 136};
int imgR1[] ={150, 0, 200};
int imgR2[] ={235, 204, 250};
int imgR3[] ={103, 25, 250};
for (int m =0; m<3; m++){
cv::Mat img = cv::imread("62.png",0);
for(int i = 0; i < img.rows; ++i) {
for(int j = 0; j < img.cols; ++j) {
int b=int(img.at<cv::Vec3b>(i,j)[0]);
int g=int(img.at<cv::Vec3b>(i,j)[1]);
int r=int(img.at<cv::Vec3b>(i,j)[2]);
int degis=0;
if(b >= imgB1[m] && b < imgB2[m] && g >= imgG1[m] && g <imgG2[m] && r >= imgR1[m] && r < imgR2[m])
{
img.at<cv::Vec3b>(i,j)[0] = imgB3[m];
img.at<cv::Vec3b>(i,j)[1] = imgG3[m];
img.at<cv::Vec3b>(i,j)[2] = imgR3[m];
}
else{
img.at<cv::Vec3b>(i,j)[0] = 0;
img.at<cv::Vec3b>(i,j)[1] = 0;
img.at<cv::Vec3b>(i,j)[2] = 0;
}
}
}
cv::namedWindow(resim[m]);
imshow(resim[m],img);
cv::waitKey(0);
imwrite(resim[m], img);
}
return 0;
}
程序停止。只有"程序已停止"的消息在黑色窗口。
<p我不熟悉openv。我不知道范围。>
我用inrange重写代码。现在它起作用了。我稍后会重新整理,但如果有人需要一个例子,我会分享它。
#include <iostream>
#include <iostream>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat src =imread("atlas40.png", CV_LOAD_IMAGE_COLOR);
Mat mask,mask2,mask3,mask4,mask5,mask6,mask7;
// Min values
int blueMin =190;
int greenMin=180;
int redMin =0;
// Max values
int blueMax =255;
int greenMax=255;
int redMax =204;
int blueMin2=0;
int greenMin2=128;
int redMin2=0;
int blueMax2=0;
int greenMax2=255;
int redMax2=191;
int blueMin3=0;
int greenMin3=8;
int redMin3=175;
int blueMax3=92;
int greenMax3=86;
int redMax3=205;
int blueMin4=0;
int greenMin4=90;
int redMin4=200;
int blueMax4=80;
int greenMax4=150;
int redMax4=255;
int blueMin5=190;
int greenMin5=90;
int redMin5=230;
int blueMax5=220;
int greenMax5=150;
int redMax5=255;
int blueMin6=50;
int greenMin6=0;
int redMin6=50;
int blueMax6=255;
int greenMax6=0;
int redMax6=255;
int blueMin7=0;
int greenMin7=200;
int redMin7=200;
int blueMax7=180;
int greenMax7=255;
int redMax7=255;
inRange(src, Scalar(blueMin, greenMin, redMin), Scalar(blueMax, greenMax, redMax), mask);
inRange(src, Scalar(blueMin2, greenMin2, redMin2), Scalar(blueMax2, greenMax2, redMax2), mask2);
inRange(src, Scalar(blueMin3, greenMin3, redMin3), Scalar(blueMax3, greenMax3, redMax3), mask3);
inRange(src, Scalar(blueMin4, greenMin4, redMin4), Scalar(blueMax4, greenMax4, redMax4), mask4);
inRange(src, Scalar(blueMin5, greenMin5, redMin5), Scalar(blueMax5, greenMax5, redMax5), mask5);
inRange(src, Scalar(blueMin6, greenMin6, redMin6), Scalar(blueMax6, greenMax6, redMax6), mask6);
inRange(src, Scalar(blueMin7, greenMin7, redMin7), Scalar(blueMax7, greenMax7, redMax7), mask7);
imshow("Color 1",mask);
imshow("Color 2",mask2);
imshow("Color 3",mask3);
imshow("Color 4",mask4);
imshow("Color 5",mask5);
imshow("Color 6",mask6);
imshow("Color 7",mask7);
int k = waitKey();
imwrite("401.png",mask);
imwrite("402.png",mask2);
imwrite("403.png",mask3);
imwrite("404.png",mask4);
imwrite("405.png",mask5);
imwrite("406.png",mask6);
imwrite("407.png",mask7);
return 0;
}
谢谢你的帮助