我在roi_corners中使用硬编码值来模糊粉红色汽车的车牌。我想通过检测上下粉色范围来找出roi_corners,以便可以通过检测粉色位置来自动将其模糊。我正在使用下面的代码,效果很好,只需要基于粉红色上下限范围以编程方式找到roi_corners的帮助。下面提供了粉色范围,以帮助您。
lower_color = np.array([158,127,0])
upper_color = np.array([179,255,255])
请在下面找到我正在使用的代码
import cv2 as cv
import numpy as np
# Here I define the list of vertices of an example polygon ROI:
roi_corners = np.array([[(34,188),(30,214),(80,227),(82,200)]],dtype = np.int32)
print ('print roi_corners ')
print (roi_corners)
print (type (roi_corners)) # <class 'numpy.ndarray'>
# Read the original Image:
image = cv.imread('image_new.jpeg')
# create a blurred copy of the entire image:
blurred_image = cv.GaussianBlur(image,(43, 43), 30)
# create a mask for the ROI and fill in the ROI with (255,255,255) color :
mask = np.zeros(image.shape, dtype=np.uint8)
channel_count = image.shape[2]
ignore_mask_color = (255,)*channel_count
cv.fillPoly(mask, roi_corners, ignore_mask_color)
# create a mask for everywhere in the original image except the ROI, (hence mask_inverse) :
mask_inverse = np.ones(mask.shape).astype(np.uint8)*255 - mask
# combine all the masks and above images in the following way :
final_image = cv.bitwise_and(blurred_image, mask) + cv.bitwise_and(image, mask_inverse)
cv.imshow("image", image)
cv.imshow("final_image", final_image)
cv.waitKey()
cv.destroyAllWindows()
最佳答案
这是在Python OpenCV中获得车牌上粉红色边界的一种方法。
- Read the input
- Threshold on the pink
- Apply morphology to clean it up
- Get the contour
- Get the rotated rectangle corners from the contour
- Draw the rotated rectangle on the input image
- Save the results
输入:
import cv2
import numpy as np
# read image
img = cv2.imread("pink_license.jpg")
# get color bounds of pink
lower =(130,0,220) # lower bound for each channel
upper = (170,255,255) # upper bound for each channel
# create the mask and use it to change the colors
thresh = cv2.inRange(img, lower, upper)
# apply morphology
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = np.ones((7,7), np.uint8)
morph = cv2.morphologyEx(morph, cv2.MORPH_DILATE, kernel)
# get contour
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
cntr = contours[0]
# get rotated rectangle from contour
rot_rect = cv2.minAreaRect(cntr)
box = cv2.boxPoints(rot_rect)
box = np.int0(box)
print(box)
# draw rotated rectangle on copy of img
rot_bbox = img.copy()
cv2.drawContours(rot_bbox,[box],0,(0,255,0),1)
# write img with red rotated bounding box to disk
cv2.imwrite("pink_license_thresh.jpg", thresh)
cv2.imwrite("pink_license_morph.jpg", morph)
cv2.imwrite("pink_license_rot_rect.png", rot_bbox)
# display it
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("MORPH", morph)
cv2.imshow("BBOX", rot_bbox)
cv2.waitKey(0)
阈值图片:
形态清除图像:
输入上的绿色旋转矩形:
角坐标:
[[ 74 212]
[ 39 209]
[ 40 197]
[ 75 200]]