如何根据较低和较高的粉红色范围找到 roi_corners,以便它可以在 python 中用 opencv 模糊
How to find roi_corners based on lower and upper pink color range so it can be blurred in python with opencv
我在 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]]
我在 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]]