gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:51:46 25 4
gpt4 key购买 nike

我正在处理示例任务,我需要在任何移动设备上突出显示不寻常的标记。我正在尝试使用 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()

任何帮助将不胜感激。谢谢你。

最佳答案

我的建议是使用自适应阈值和区域过滤(可能还有其他特征)。这是我使用 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 图像对齐,并创建相机和 Logo 等标记的 mask ,以便您可以过滤结果以排除那些(甚至可能是相机轮廓的边框) .

关于python - 如何突出显示移动设备上的异常标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com