使用OpenCV提取图像的一部分

6

我对图像处理新手,想知道是否有办法从图像中提取星星标志?

enter image description here


仅从这张图片中?裁剪到相关坐标。如果您有多个图像,情况会变得更加复杂...我可能会尝试某种霍夫线或形状检测器,或者在归一化和otsu局部阈值化图像上进行模板匹配。如果您的数据集足够庞大,甚至可以尝试像YOLO这样的SOTA对象检测模型。 - Mateen Ulhaq
我们可以使用算术和逻辑运算来完成这个任务吗? - Pasindu Weerasinghe
1
只需在图像编辑程序中测量坐标,然后执行 star_img = img[y1:y2, x1:x2] - Mateen Ulhaq
4个回答

4
如果您想使用OpenCV的裁剪工具裁剪标志,请参考以下步骤:
import cv2
img = cv2.imread("fight plane.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

获取坐标非常容易。这里有另一个相关问题 - 如何使用Python在OpenCV中裁剪图像


3

试一试吧

import cv2 as cv
import numpy as np

img = cv.imread('airplane.png')
cv.imshow('Original', img)

blank = np.zeros(img.shape[:2], dtype='uint8')

center_coordinates = (144, 233)

radius = 10

mask = cv.circle(blank, center_coordinates, radius, 255, -1)

masked = cv.bitwise_and(img, img, mask=mask)
cv.imshow('Output', masked)

cv.waitKey(0)


2
使用 cv2.selectROI 函数。
cv2.namedWindow('ROI') 
# define area by mouse
r=cv2.selectROI('ROI', img,False,False)
imROI = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
cv2.destroyWindow('ROI')
cv2.imshow("ROI", imROI)
cv2.waitKey(0)
cv2.destroyAllWindows()

0
import cv2
import argparse

ref_point = []
crop = False

def shape_selection(event, x, y, flags, param):
    global ref_point, crop

    if event == cv2.EVENT_LBUTTONDOWN:
        ref_point = [(x, y)]
    elif event == cv2.EVENT_LBUTTONUP:
        ref_point.append((x, y))
        cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)
        cv2.imshow("image", image)


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help ="Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", shape_selection)
while True:
    cv2.imshow("image", image)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("r"):
        image = clone.copy()
    elif key == ord("c"):
        break
if len(ref_point) == 2:
    crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:                                                        ref_point[1][0]]
    cv2.imshow("crop_img", crop_img)
    cv2.waitKey(0)
cv2.destroyAllWindows()

试一试,它会有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接