不能單獨的對象我有3個辣椒這一形象:在C++ OpenCV的HSV圖像分離
我需要這個圖片HSV形式轉化,然後將每個花椒放入自己的形象分開。
到目前爲止,我似乎能夠很好地將紅辣椒與其他紅辣椒分開。但是,我似乎無法弄清楚如何分開其他辣椒。
這裏是我的代碼:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <string>
#include <vector>
int main(int argc, char *argv[]){
cv::Mat im_in;
cv::Mat hsv_in;
cv::Mat bgr_in;
cv::Mat orig_in;
cv::Mat im_o1;
cv::Mat im_o2;
cv::Mat im_o3;
// Read image
if (argc<2){
im_in = cv::imread("colorpeppers.jpg");
}
if (argc==2){
im_in=cv::imread((argv[1]));
}
if (argc>2){
std::cout<<"Error! Too many arguments!"<<std::endl;
}
if (im_in.empty()){
std::cout << "error detected. something went wrong with opening the image. is it empty? exiting"<<std::endl;
return -1;
}
cv::Mat orig_image = im_in.clone();
cv::medianBlur(im_in, im_in, 3);
cv::cvtColor(im_in, hsv_in, cv::COLOR_BGR2HSV);
cv::namedWindow("Original Image", cv::WINDOW_AUTOSIZE);
cv::imshow("Orginal Image", im_in);
cv::namedWindow("Orginal Image converted to HSV", cv::WINDOW_AUTOSIZE);
cv::imshow("Original Image converted to HSV", hsv_in);
cv::Mat lower_red_hue_range;
cv::Mat upper_red_hue_range;
cv::Mat lower_green_hue_range;
cv::Mat upper_green_hue_range;
cv::Mat lower_yellow_hue_range;
cv::Mat upper_yellow_hue_range;
cv::inRange(hsv_in, cv::Scalar(0,100,100), cv::Scalar(10,255,255), lower_red_hue_range);
cv::inRange(hsv_in, cv::Scalar(160,100,100),cv::Scalar(179, 255, 255), upper_red_hue_range);
cv::inRange(hsv_in, cv::Scalar(0,100,100), cv::Scalar(10,255,255), lower_green_hue_range);
cv::inRange(hsv_in, cv::Scalar(50,100,100),cv::Scalar(70,255,255), upper_green_hue_range);
cv::inRange(hsv_in, cv::Scalar(0,100,100),cv::Scalar(20,0,0),lower_yellow_hue_range);
cv::inRange(hsv_in,cv::Scalar(10,100,100),cv::Scalar(50,110,110),upper_yellow_hue_range);
//combining the above
cv::Mat red_hue_image=im_in.clone();
cv::Mat green_hue_image=im_in.clone();
cv::Mat yellow_hue_image=im_in.clone();
cv::addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
cv::addWeighted(lower_green_hue_range, 1.0, upper_green_hue_range, 1.0, 0.0, green_hue_image);
cv::addWeighted(lower_yellow_hue_range, 1.0, upper_yellow_hue_range, 1.0, 0.0, yellow_hue_image);
cv::GaussianBlur(red_hue_image, red_hue_image, cv::Size(9,9), 2, 2);
cv::GaussianBlur(green_hue_image, green_hue_image, cv::Size(9,9),2,2);
cv::GaussianBlur(yellow_hue_image, yellow_hue_image, cv::Size(9,9),2,2);
cv::namedWindow("Threshold lower red image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold lower red image", lower_red_hue_range);
cv::namedWindow("Threshold upper red image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold upper red image", upper_red_hue_range);
cv::namedWindow("Combined Threshold red Images", cv::WINDOW_AUTOSIZE);
cv::imshow("Combined Threshold red Images", red_hue_image);
cv::namedWindow("Threshold lower green image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold lower green image", lower_green_hue_range);
cv::namedWindow("Threshold upper green image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold upper green image", upper_green_hue_range);
cv::namedWindow("Combined Threshold green Images", cv::WINDOW_AUTOSIZE);
cv::imshow("Combined Threshold green Images", green_hue_image);
cv::namedWindow("Threshold lower yellow image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold lower yellow image", lower_yellow_hue_range);
cv::namedWindow("Threshold upper yellow image", cv::WINDOW_AUTOSIZE);
cv::imshow("Threshold upper yellow image", upper_yellow_hue_range);
cv::namedWindow("Combined Threshold yellow Images", cv::WINDOW_AUTOSIZE);
cv::imshow("Combined Threshold yellow Images", yellow_hue_image);
//cv::Mat redpepper;//=red_hue_image.clone();
//cv::Mat redpepper_hsv;
//cvtColor(redpepper, redpepper_hsv,CV_BGR2HSV);
// for(int i = 0; i < redpepper.rows;i++){
// for(int j = 0; j <redpepper.cols;j++){
char k;
for (int x=1;x<15;x++){
k=cvWaitKey(0);
}
}
[GIMP的最大RGB](http://docs.gimp.org/en/plug-in-max-rgb。HTML)似乎值得去嘗試 – sturkmen