Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv:如何在图像中找到使用最少的颜色_Opencv - Fatal编程技术网

Opencv:如何在图像中找到使用最少的颜色

Opencv:如何在图像中找到使用最少的颜色,opencv,Opencv,找到图像中使用最少颜色的算法是什么?我是opencv的新手 我的代码在阅读后回复。 我怎样才能改进它?另外,我如何检查它是否适用于“最少使用的颜色” int main(int argc, char* argv[]) { // Load the file char* filename = argv[1]; cv::Mat matImage = cv::imread(filename); if(!matImage.data) { printf("Unable to open the fil

找到图像中使用最少颜色的算法是什么?我是opencv的新手

我的代码在阅读后回复。 我怎样才能改进它?另外,我如何检查它是否适用于“最少使用的颜色”

int main(int argc, char* argv[]) {
// Load the file
char* filename = argv[1];
cv::Mat matImage = cv::imread(filename);

if(!matImage.data) {
    printf("Unable to open the file: %s\n", filename);
    return 1;
}
const int width = matImage.cols;
const int height = matImage.rows;
printf("\n height -> %d , width -> %d\n",height,width);
map<string,int> mymap;
int c = 1;
for(int y=0;y<height;y++) {
    cv::Vec3b* ptr = matImage.ptr<cv::Vec3b>(y);
    for(int x=0;x<width;x++) {
        cv::Vec3b color = ptr[x];
        char buff[16];
        sprintf(buff,"#%02x%02x%02x",color[0],color[1],color[2]);
        string s(buff); 
        if(mymap.find(s) == mymap.end()){
            mymap[s] = 1;
        }
        else 
            mymap[s] = mymap[s] + 1;
    }
}
cout<<"color count = "<<mymap.size()<<endl;
int max = 0;
int min = mymap.size(); 
string strmax,strmin;
for (std::map<string,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it){
        if(it->second > max)
        {
            max = it->second;
            strmax = it->first;
        }
        if(it->second < min)
        {
            min = it->second;
            strmin = it->first;
        }
    }
    cout<<"most used color -> "<<strmax<<" count -> "<<max<<endl;
    cout<<"least used color -> "<<strmin<<" count -> "<<min<<endl;
return 0;
}
intmain(intargc,char*argv[]){
//加载文件
char*filename=argv[1];
cv::Mat matImage=cv::imread(文件名);
如果(!matImage.data){
printf(“无法打开文件:%s\n”,文件名);
返回1;
}
const int width=matImage.cols;
const int height=matImage.rows;
printf(“\n高度->%d,宽度->%d\n”,高度,宽度);
地图我的地图;
int c=1;

对于(int y=0;y图像中的红色、绿色和蓝色的原色可以通过openCV中的以下过程轻松检测

split((Image),channels);  
Mat red, blue, green;

inRange(channels[0], Scalar(50), Scalar(255), blue); // blue
inRange(channels[1], Scalar(50), Scalar(255), green); // green
inRange(channels[2], Scalar(50), Scalar(255), red); // red

double image_size = abs((Image).cols * (Image).rows);  // Total no of pixels in the image
double red_percent = ((double) cv::countNonZero(red))/image_size;
double blue_percent = ((double) cv::countNonZero(green))/image_size;
double green_percent = ((double) cv::countNonZero(blue))/image_size;

我想你可以创建一个1600万条目的数组,通过
(RWelcome to StackOverflow!请更详细地描述您试图解决的问题。您有什么类型的图像,如何定义颜色?以及,到目前为止您尝试了什么代码?一些颜色可能并不完全存在。它们是最不常用的吗?我想您可以创建一个1600万条目的数组,按(RY您的代码看起来非常合理。您可以使用ImageMagick检查颜色出现的频率,这在大多数Linux发行版以及macOS和Windows上都可用。在终端中使用此命令
convert YourImage.png-define histogram:unique colors=true-format%c histogram:info:-
我将所有建议放在一起作为答案因此,人们不必在评论中胡乱琢磨。问题是如何找到使用最少的颜色。你如何回答?任何颜色都是由这些组合形成的,如果红色的百分比最小,那么图像中与红色相关的颜色(粉色、橙色等)使用最少。问题是关于使用最少的颜色(例如#fcbb33)不仅仅是原色。知道红色是最不常用的主要颜色如何帮助找到真正最不常用的颜色(暗红色、浅红色、砖红色)?但是我如何找到最不常用的颜色(确切的颜色,在上面的评论中询问)例如,要找到使用最多的颜色或主色,我们可以使用k-均值算法。我们可以使用什么算法来找到使用最少的颜色?
convert YourImage.png -define histogram:unique-colors=true -format %c histogram:info:- | sort -n | head -10
    35: (104, 89,206) #6859CE srgb(104,89,206)
    39: (109, 83, 47) #6D532F srgb(109,83,47)
    51: (103, 73,135) #674987 srgb(103,73,135)
    59: (214,120,103) #D67867 srgb(214,120,103)
    59: (223,224,228) #DFE0E4 srgb(223,224,228)
    63: (202,115,117) #CA7375 srgb(202,115,117)
    67: (205,150, 72) #CD9648 srgb(205,150,72)
    71: (230,164, 96) #E6A460 srgb(230,164,96)
    75: (173,149, 83) #AD9553 srgb(173,149,83)
    77: (182,134, 55) #B68637 srgb(182,134,55)
magick YourImage.png -define histogram:unique-colors=true -format %c histogram:info:- | sort -n | head -10