图像裁剪以获得图像中的特定形状

时间:2018-03-06 06:54:18

标签: python-3.x image opencv

[我有如下图像,我需要从所有图像中提取白色条带部分。 我已经尝试使用PIL通过手动指定像素值来提取矩形部分,是否可以通过任何自动方式完成此工作,只需通过喂食图像就可以返回矩形部分

以下是我的剪辑代码:

from PIL import Image
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = Image.open('C:/Users/ShAgarwal/Documents/image_dataset/pic9.jpg')
half_the_width = img.size[0] / 2
half_the_height = img.size[1] / 2

img4 = img.crop(
    (
        half_the_width-1632,
        half_the_height - 440,
        half_the_width+1632,
        half_the_height + 80
    )
)

sample image

1 个答案:

答案 0 :(得分:0)

import cv2
import numpy as np
from matplotlib import pyplot as plt
image='IMG_3134.JPG'
# read image
imgc = cv2.imread(image)
img = cv2.resize(imgc, None, fx=0.25, fy=0.25) # resize since image is huge
#cropping the strip dimensions
#crop_img = img[1010:1650,140:1099723]
blurred = cv2.blur(img, (3,3))
canny = cv2.Canny(blurred, 50, 200)

使用canny算法通过自动图像检测标记坐标

## find the non-zero min-max coords of canny
pts = np.argwhere(canny>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)`
`## crop the region
cropped = img[y1:y2, x1:x2]
cv2.imwrite("cropped.png", cropped)
#Select the bounded area around white boundary
tagged = cv2.rectangle(img.copy(), (x1,y1), (x2,y2), (0,255,0), 3, cv2.LINE_AA)
r = cv2.selectROI(tagged)
imCrop = im[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
#Bounded Area
cv2.imwrite("taggd2.png", imcrop)
cv2.waitKey()

Results from above code

相关问题