简体   繁体   English

从具有轮廓的图像中提取 ROI

[英]Extracting the ROI from image with contour

I have bunch of pictures like this :我有一堆这样的图片:

And want to extract this part :并想提取这部分

so first i use masking the red part, secondly i threshold and contour.所以首先我使用掩盖红色部分,其次我使用阈值和轮廓。 But still cant select the only lip part.但仍然不能 select 唯一的唇部。

import cv2 
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("C:/Users/TBD/Desktop/TEZ GORSEL/098.JPG")

img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_red = np.array([0,95,50])
upper_red = np.array([2,255,255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)

lower_red = np.array([175,95,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

mask = mask0+mask1
output_img = img.copy()
output_img[np.where(mask==0)] = 0

output_hsv = img_hsv.copy()
output_hsv[np.where(mask==0)] = 0
son = cv2.cvtColor(output_hsv, cv2.COLOR_HSV2BGR)
gray_son = cv2.cvtColor(son, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray_son, 10, 255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    if w>1960 and h>800:
        break
        
cropped = son[y:y+h, x:x+w]
cv2.imwrite('cropped.jpg', cropped)

after these code i can reach this在这些代码之后我可以达到这个

But still i cannot extract the lip only.但我仍然不能只提取嘴唇。 What should i do next?接下来我该怎么办?

With classical image processing as your solution, this will change the results if the environmental conditions change.使用经典图像处理作为您的解决方案,如果环境条件发生变化,这将改变结果。 Using face landmarks you can get detailed information and crop it from the image as a rectangle.使用面部地标,您可以获得详细信息并将其从图像中裁剪为矩形。

Other solution can be that firstly, you can detect mouth using in below afterwards, you can apply your solution method or lip detect.其他解决方案可以是首先,您可以使用下面的检测嘴,然后您可以应用您的解决方法或唇检测。

import cv2
import numpy as np

mouth_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_mouth.xml')

if mouth_cascade.empty():
  raise IOError('Unable to load the mouth cascade classifier xml file')

cap = cv2.VideoCapture(0)
ds_factor = 0.5

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    mouth_rects = mouth_cascade.detectMultiScale(gray, 1.7, 11)
    for (x,y,w,h) in mouth_rects:
        y = int(y - 0.15*h)
        cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
        break

    cv2.imshow('Mouth Detector', frame)

    c = cv2.waitKey(1)
    if c == 27:
        break

cap.release()
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM