获取图片某种颜色所占百分比
生活随笔
收集整理的這篇文章主要介紹了
获取图片某种颜色所占百分比
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取藍色在圖片中所占百分比
- 思路
- 代碼
- 運行結果
- 測試圖片
思路
由于每種顏色都有一個閾值,單純的計算RGB百分比的效果并不好,對于這點,網上已經有很多結論。于是,我想到采用hsv空間下每個像素點分量,獲取某種顏色所占百分比(我這里采用的是藍色)。因為,在hsv空間下,相近顏色都會被歸于一個范圍。所以,我們可以比較相近顏色,在圖片中,所占百分比。
代碼
#include <iostream> #include <opencv2/opencv.hpp> #include <iomanip> #include <cmath> #include <string> #include <opencv2/highgui/highgui_c.h> #include <unordered_map> #include <vector> using namespace std; using namespace cv; //struct Triple //{ // char col[_MAX_PATH]; // int h_min; // int s_min; // int v_min; // int h_max; // int s_max; // int v_max; //}; /* 區分不同顏色,在圖片中所占百分比 */ unordered_map<string,float> hsv_percentage(Mat hsv_image) {int num_black = 0;int num_grey = 0;int num_white = 0;int num_red = 0;int num_orange = 0;int num_yellow = 0;int num_green = 0;int num_blue = 0;int num_purple = 0;//vector<float> record(9,0);unordered_map<string, float> record;float area = (float)(hsv_image.rows * hsv_image.cols);for (int i = 0; i < hsv_image.rows; i++) //行數{for (int j = 0; j < hsv_image.cols; j++) //列數{//在這里操作具體元素//{"black", 0, 0, 0, 180, 255, 46}if ((hsv_image.at<Vec3b>(i, j)[0] >= 0 && hsv_image.at<Vec3b>(i, j)[0] <= 180)&& (hsv_image.at<Vec3b>(i, j)[1] >= 0 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 0 && hsv_image.at<Vec3b>(i, j)[2] <= 46)) {num_black++;}//{ "grey",0,0,46,180,43,220 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 0 && hsv_image.at<Vec3b>(i, j)[0] <= 180)&& (hsv_image.at<Vec3b>(i, j)[1] >= 0 && hsv_image.at<Vec3b>(i, j)[1] <= 43)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 220)) {num_grey++;}// { "white",0,0,221,180,30,255 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 0 && hsv_image.at<Vec3b>(i, j)[0] <= 180)&& (hsv_image.at<Vec3b>(i, j)[1] >= 0 && hsv_image.at<Vec3b>(i, j)[1] <= 30)&& (hsv_image.at<Vec3b>(i, j)[2] >= 221 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_white++;}//{ "red", 156, 43, 46, 180, 255, 255 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 156 && hsv_image.at<Vec3b>(i, j)[0] <= 180)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_red++;}else if ((hsv_image.at<Vec3b>(i, j)[0] >= 0&& hsv_image.at<Vec3b>(i, j)[0] <= 10)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_red++;}//{ "orange",11,43,46,25,255,255 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 11 && hsv_image.at<Vec3b>(i, j)[0] <= 25)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_orange++;}// { "yellow", 26, 43, 46, 34, 255, 255 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 26 && hsv_image.at<Vec3b>(i, j)[0] <= 34)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_yellow++;}//{ "green",35,43,46,99,255,255 },else if ((hsv_image.at<Vec3b>(i, j)[0] >= 35 && hsv_image.at<Vec3b>(i, j)[0] <= 99)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_green++;}//{ "blue",100,43,46,124,255,255 },else if ((hsv_image.at<Vec3b>(i, j)[0] >= 100 && hsv_image.at<Vec3b>(i, j)[0] <= 124)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_blue++;}// { "purple",125,43,46,155,255,255 }else if ((hsv_image.at<Vec3b>(i, j)[0] >= 125 && hsv_image.at<Vec3b>(i, j)[0] <= 155)&& (hsv_image.at<Vec3b>(i, j)[1] >= 43 && hsv_image.at<Vec3b>(i, j)[1] <= 255)&& (hsv_image.at<Vec3b>(i, j)[2] >= 46 && hsv_image.at<Vec3b>(i, j)[2] <= 255)) {num_purple++;}}}record.insert(make_pair("black", (float)num_black / area));record.insert(make_pair("grey", (float)num_grey / area));record.insert(make_pair("white", (float)num_white / area));record.insert(make_pair("red", (float)num_red / area));record.insert(make_pair("orange", (float)num_orange / area));record.insert(make_pair("yellow", (float)num_yellow / area));record.insert(make_pair("green", (float)num_green / area));record.insert(make_pair("blue", (float)num_blue / area));record.insert(make_pair("purple", (float)num_purple / area));//(float)num / (float)(hsv_image.rows * hsv_image.cols);return record; } /* 在圖片里查找指定顏色的比例 */ void Mat_color_Find(string qimage) {Mat image = imread(qimage);//將圖片加載進來if (image.empty()){cout << "could not load image...\n" << endl;return;}Mat hsv_image;cvtColor(image, hsv_image, CV_BGR2HSV);unordered_map<string, float> temp = hsv_percentage(hsv_image);//rat = (float)num / (float)(hsv_image.rows * hsv_image.cols);for (auto it = temp.begin(); it != temp.end();it++) {cout << it->first << " " <<it->second << endl;}return; } int main(){/*struct Triple colour[9] ={{"black",0,0,0,180,255,46},{"grey",0,0,46,180,43,220},{"white",0,0,221,180,30,255},{"red",156,43,46,180,255,255},{"orange",11,43,46,25,255,255},{"yellow",26,43,46,34,255,255},{"green",35,43,46,99,255,255},{"blue",100,43,46,124,255,255},{"purple",125,43,46,155,255,255}};*/string s = "C:/Users/wenhaofu/Desktop/picture/2021-07-01.png";Mat_color_Find(s);//cout << blue_temp << endl; }運行結果
測試圖片
獲取其中各種顏色所占百分比
總結
以上是生活随笔為你收集整理的获取图片某种颜色所占百分比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何解决Win10系统更新显示0x800
- 下一篇: IIS6 + Resin3.1.x 的不