对于你可以简单地做一个RGB图像计数蓝色像素以下
- Inrange源图像到蓝色分量滤除,以二值图像。
- 使用函数countNonZero计算二值图像中的非零像素。
您可以参考下面的C++代码,该怎么办呢
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat src,dst;
src = imread("rgb1.jpg",1);
inRange(src, Scalar(200,0,0), Scalar(255,50,50), dst); //In range with approximate blue range
cout<<"No of blue pixels---->"<<countNonZero(dst)<<endl;
imshow("src",src);
imshow("out",out);
waitKey(0);
return 0;
}
编辑: -
这里是工作的Python代码
import cv2
import numpy as np
img = cv2.imread("bgr.png")
BLUE_MIN = np.array([0, 0, 200], np.uint8)
BLUE_MAX = np.array([50, 50, 255], np.uint8)
dst = cv2.inRange(img, BLUE_MIN, BLUE_MAX)
no_blue = cv2.countNonZero(dst)
print('The number of blue pixels is: ' + str(no_blue))
cv2.namedWindow("opencv")
cv2.imshow("opencv",img)
cv2.waitKey(0)
希望这是什么您正在寻找.....
EDIT2: -
正如@ kigurai下面评论认为OpenCV的图像中BGR为了我给错为了BLUE_MIN和BLUE_MAX阵列。 所以在上面的代码行
BLUE_MIN = np.array([0, 0, 200], np.uint8)
BLUE_MAX = np.array([50, 50, 255], np.uint8)
应改为
BLUE_MIN = np.array([200, 0, 0], np.uint8) // minimum value of blue pixel in BGR order
BLUE_MAX = np.array([255, 50, 50], np.uint8)// maximum value of blue pixel in BGR order
欢迎来到Stack Overflow!我们不可能在没有看到更多背景的情况下为您提供帮助。你可以在这里发布你的实际代码吗?[描述你试过的东西](http://whathaveyoutried.com)?请务必检查[问题清单](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)。谢谢! –
您需要定义“蓝色像素”的含义。 –