2017-04-26 26 views
0

我是opencv的新手,我一直在用CIEXYZ做皮膚檢測。但是我得到了將RGB轉換爲CIE Lab以獲得膚色的區域,我根據this從RGB中進行了一些計算。如何使用ciexyz/cielab檢測膚色?

原始圖像

enter image description here

結果是什麼螺母黑幀。從RGB其CIEXYZ enter image description here

,這是二值圖像

enter image description here

,但我想這樣

enter image description here

顯示它下面是我的源代碼:

Mat img_color_space = new Mat(); 
Mat mask = new Mat(); 

Imgproc.cvtColor(src, img_color_space, colorBgr2hsv); 
Imgcodecs.imwrite(path+"CIELAB/hsv.jpg",img_color_space); 
Imgproc.blur(img_color_space, img_color_space, new Size(3,3)); 
Mat canny_output = new Mat(); 

Scalar minValues = new Scalar(0,10,60); 
Scalar maxValues = new Scalar(20,150,255); 
// show the current selected HSV range 
String valuesToPrint = "Hue range: " + minValues.val[0] + "-" + maxValues.val[0] 
     + "\tSaturation range: " + minValues.val[1] + "-" + maxValues.val[1] + "\tValue range: " 
     + minValues.val[2] + "-" + maxValues.val[2]; 
//System.out.println("tresholding:"+valuesToPrint); 

Core.inRange(img_color_space, minValues, maxValues, mask); 
Imgcodecs.imwrite(path+"CIELAB/mask.jpg",mask); 
List<MatOfPoint> contours = new ArrayList<>(); 
Mat hierarchy = new Mat(); 

Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE,new Point(0,0)); 
int s = findBiggestContour(contours); 

Mat drawing = Mat.zeros(mask.size(), CvType.CV_8UC3); 
Imgproc.drawContours(drawing, contours, s, new Scalar(255, 255, 255), -1,8,hierarchy,0,new Point(0,0)); 
Imgproc.blur(drawing, drawing, new Size(3,3)); 
Imgcodecs.imwrite(path+"CIELAB/biggest.jpg",drawing); 

我的代碼有問題嗎?提前致謝!

回答

1

您可以以更簡單的方式分割手。

按照原樣讀取CIELAB圖像並將其分爲三個不同的通道。分別分析每個通道,看看哪一個最好。之後應用門檻。

以下代碼是在python其可以轉換爲Java:

import cv2 

filename = 'hand.jpg' 
img = cv2.imread(filename) 
blue_channel, green_channel, red_channel = cv2.split(img) 
cv2.imshow('green_channel', green_channel) 

這是圖像的綠色通道:

enter image description here

#---I split the image in blue, green and red channels because the image I saved is in BGR format ---# 

#---I applied binary threshold to the green channel---# 
ret, thresh = cv2.threshold(g, 152, 255, 1) 
cv2.imshow('thresh', thresh) 
#--- I got the following----# 

enter image description here

現在你可以找到最大的輪廓和s孤獨的手

+1

非常感謝你。 U'r天才兄弟.. –

+0

@FebryFairuz很高興我可以幫助 –