简体   繁体   English

如何根据较低和较高的粉红色范围找到 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

I am using hardcoded values in roi_corners to blur number plate of car which is in pink color.我在 roi_corners 中使用硬编码值来模糊粉红色汽车的车牌号。 I want to find out roi_corners by detecting lower and upper pink color range so that it can be blurred automatically by detecting location of pink color.我想通过检测较低和较高的粉红色范围来找出 roi_corners,以便可以通过检测粉红色的位置自动模糊它。 I am using below code which works fine, only need help in finding roi_corners programmatically based on pink color lower and upper range.我正在使用下面的代码,它工作正常,只需要帮助根据粉红色的下限和上限以编程方式找到 roi_corners。 Pink color range is provided below for your help.下面提供了粉红色的颜色范围以供您参考。 lower_color = np.array([158, 127, 0]) upper_color = np.array([179, 255, 255]) lower_color = np.array([158, 127, 0]) upper_color = np.array([179, 255, 255])

Please find below code which I am using请在下面找到我正在使用的代码

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()

在此处输入图像描述

Here is one way to get the bounds of the pink on the license plate in Python OpenCV.这是获取车牌上粉红色边界的一种方法 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

Input:输入:

在此处输入图像描述

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)

Threshold image:阈值图像:

在此处输入图像描述

Morphology cleaned image:形态学清洁图像:

在此处输入图像描述

Green Rotated Rectangle on input:输入上的绿色旋转矩形:

在此处输入图像描述

Corner Coordinates:角坐标:

[[ 74 212]
 [ 39 209]
 [ 40 197]
 [ 75 200]]

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

相关问题 如何找到 colors 颜色的“上”和“下”范围? - How to find a colors “Upper” and “Lower” range of a color? 如何在HSV opencv python中指定颜色的上限值和下限值 - how to i specify the upper and the lower value of a color in HSV opencv python 如何使用 opencv 和 Python 在 ROI 内找到轮廓? - How can I find contours inside ROI using opencv and Python? 如何通过for循环根据Python中的范围创建下限值和上限值列表? - How to create a list of lower and upper bound values based on range in Python via for loop? 如何使用范围将列表中的单词更改为大写/小写-Python - How to change words in a list to upper/lower case using range - Python Python - 如何找到下三角形numpy矩阵的方阵? (对称的上三角形) - Python — How can I find the square matrix of a lower triangular numpy matrix? (with a symmetrical upper triangle) 如何在python 3中找到在字符串中找到大小写字符的分组 - How to find find a grouping of upper and lower case characters in a string in python 3 如何在opencv-python中的单独图像中选择ROI? - How can I select ROI in a separate image in opencv-python? 如何在OpenCV / Python中获得可拖动/可调的ROI? - How can I make a draggable/adjustable ROI in OpenCV/Python? 如何在 python 和 opencv 中移动 ROI 的顶点? - How can i move the vertices of the ROI in python and opencv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM