如何突出显示移动设备上的异常标记

2024-09-30 08:21:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个示例任务,我需要突出显示任何移动设备上的异常标记。我正在尝试使用opencv python。但是,我不会因为这些不寻常的标记而得到实际的控制

输入图像如下所示:

enter image description here

输出图像如下所示: enter image description here

我正在尝试下面的方法,但没有成功

import cv2
from matplotlib import pyplot as plt

blurValue = 15
img_path = "input.jpg"

# reading the image 
image = cv2.imread(img_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0)
edged = cv2.Canny(image, 100, 255)

#applying closing function 
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
lower = np.array([4, 20, 93])
upper = np.array([83, 79, 166])

# hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# blur = cv2.GaussianBlur(hsv, (blurValue, blurValue), 0)

mask = cv2.inRange(closed, lower, upper)
result_1 = cv2.bitwise_and(frame, frame, mask = mask)
cnts = cv2.findContours(result_1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
plt.imshow(image)
plt.title("image")
plt.show()

任何帮助都将不胜感激。多谢各位


Tags: path标记图像imageimportimgpltmask
1条回答
网友
1楼 · 发布于 2024-09-30 08:21:10

我的建议是对面积(可能还有其他特征)使用自适应阈值和过滤器。下面是我使用Python OpenCV的代码和结果

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread("iphone.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# apply gaussian blur
blur = cv2.GaussianBlur(gray, (29,29), 0)

# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 51, 3)

# apply morphology open then close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 17))
open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel)

# Get contours
cnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
result = img.copy()
for c in cnts:
    area = cv2.contourArea(c)
    if area < 10000 and area > 5000:
        cv2.drawContours(result, [c], -1, (0, 255, 0), 2)


# write results to disk
cv2.imwrite("iphone_thresh.jpg", thresh)
cv2.imwrite("iphone_close.jpg", close)
cv2.imwrite("iphone_markings.jpg", result)

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("CLOSED", close)
cv2.imshow("RESULT", result)
cv2.waitKey(0)


阈值图像:

enter image description here

形态学处理图像:

enter image description here

最终结果:

enter image description here

我还建议您将图像与已知的干净iPhone图像对齐,并创建相机的遮罩和徽标等标记,以便您可以过滤结果以排除这些标记(甚至可能是相机轮廓的边框)

相关问题 更多 >

    热门问题