





附上代码:
Python代码:
import cv2
import numpy as np
def redFind(image):
lower = np.array([168, 43, 46])
upper = np.array([180, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("红色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
def purpleFind(image):
lower = np.array([142, 43, 46])
upper = np.array([147, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("紫色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
def blueFind(image):
lower = np.array([102, 43, 46])
upper = np.array([112, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("蓝色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
def yellowFind(image):
lower = np.array([20, 43, 46])
upper = np.array([25, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("黄色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
def orangeFind(image):
lower = np.array([11, 43, 46])
upper = np.array([15, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("橙色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
def brownFind(image):
lower = np.array([3, 43, 46])
upper = np.array([8, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
s = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
openI = cv2.morphologyEx(mask,cv2.MORPH_OPEN,s,iterations=1)
closeI = cv2.morphologyEx(openI,cv2.MORPH_CLOSE,s,iterations=1)
contours, hierarchy = cv2.findContours(closeI, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.minAreaRect(c)
print("棕色:", int((rect[0][1] + rect[1][1]/2)))
cv2.imshow("aaa", closeI)
cv2.waitKey(1000)
if __name__ == "__main__":
image = cv2.imread("ceshi.png")
purpleFind(image)
orangeFind(image)
yellowFind(image)
brownFind(image)
blueFind(image)
redFind(image)
C++代码:
#include "pch.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
double purpleFind(Mat image);
double orangeFind(Mat image);
double yellowFind(Mat image);
double brownFind(Mat image);
double blueFind(Mat image);
double redFind(Mat image);
struct returnData
{
double colorNumber[10];
double line_y[10];
};
int main()
{
Mat srcImage = imread("D:\\code\\code\\ceshi.png");
Mat srcImage1;
cvtColor(srcImage, srcImage1, COLOR_BGR2HSV);
double y0, y1, y2, y3, y4, y5;
returnData colorline;
y0 = purpleFind(srcImage1);
colorline.colorNumber[0] = 0;
colorline.line_y[0] = y0;
y1 = orangeFind(srcImage1);
colorline.colorNumber[1] = 1;
colorline.line_y[1] = y1;
y2 = yellowFind(srcImage1);
colorline.colorNumber[2] = 2;
colorline.line_y[2] = y2;
y3 = brownFind(srcImage1);
colorline.colorNumber[3] = 3;
colorline.line_y[3] = y3;
y4 = blueFind(srcImage1);
colorline.colorNumber[4] = 4;
colorline.line_y[4] = y4;
y5 = redFind(srcImage1);
colorline.colorNumber[5] = 5;
colorline.line_y[5] = y5;
for (int i = 0; i < 6; i++) {
cout << colorline.colorNumber[i] << endl;
cout << colorline.line_y[i] << endl;
}
return 1;
}
double purpleFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(142, 43, 46), Scalar(147, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}
double orangeFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(11, 43, 46), Scalar(15, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(7, 7));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}
double yellowFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(20, 43, 46), Scalar(25, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}
double brownFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(3, 43, 46), Scalar(8, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}
double blueFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(102, 43, 46), Scalar(112, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}
double redFind(Mat image)
{
Mat dstImage;
inRange(image, Scalar(168, 43, 46), Scalar(180, 255, 255), dstImage);
Mat s, openImage, closeImage;
s = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(dstImage, openImage, MORPH_OPEN, s, Point(-1, -1), 1);
morphologyEx(openImage, closeImage, MORPH_CLOSE, s, Point(-1, -1), 1);
vector<vector<Point>> contours;
findContours(closeImage, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Rect rect;
rect = boundingRect(contours[0]);
int line_y;
line_y = rect.y + rect.height / 2;
cout << line_y << endl;
return line_y;
}