裁剪白色pap标志

2024-04-25 01:04:38 发布

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

我有一个巨大的图像数据集,在白纸上的任意位置有一些徽标。如何使用python从图像中检索对象的坐标(左上和右下)?你知道吗

例如,考虑一下这个图像 http://ak9.picdn.net/shutterstock/videos/5360279/thumb/3.jpg(忽略阴影) 我想在图片中突出显示鸡蛋。你知道吗

编辑: 图像的分辨率很高,数量非常庞大,因此迭代求解需要大量的时间。我遗漏的一点是,图像是以1位模式存储的。所以我认为我们可以用numpy得到更好的解决方案。你知道吗


Tags: 数据对象图像httpnetvideosjpg阴影
2条回答

对于此图像(白色背景上的鸡蛋): enter image description here

您可以通过以下步骤进行裁剪:

  1. Read and convert to gray
  2. Threshold and Invert
  3. Find the extreme coordinates and crop

卵子图像,大小为(480, 852, 3),成本为0.016s。你知道吗


代码:

## Time passed: 0.016 s

#!/usr/bin/python3
# 2018/04/10 19:39:14
# 2018/04/10 20:25:36 
import cv2
import numpy as np
import matplotlib.pyplot as plt

import time
ts = time.time()

## 1. Read and convert to gray
fname = "egg.jpg"
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

##  2. Threshold and Invert
th, dst = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)

##  3. Find the extreme coordinates and crop 
ys, xs = np.where(dst>0)
target = img[ys.min():ys.max(), xs.min():xs.max()]

te = time.time()
print("Time passed: {:.3f} s".format(te-ts))
plt.imshow(target)
plt.show()

## Time passed: 0.016 s

enter image description here

如果图片的其余部分是一种颜色,您可以比较每个像素,并找到一种不同的颜色来指示图片的开始,例如请注意,我假设右上角是背景色,如果情况并非总是如此,请使用不同的方法(例如计数模式像素颜色)!

import numpy as np
from PIL import Image
import pprint

def get_y_top(pix, width, height, background, difference):
    back_np = np.array(background)
    for y in range(0, height):
        for x in range(0, width):
            if max(np.abs(np.array(pix[x, y]) - back_np)) > difference:
                return y

def get_y_bot(pix, width, height, background, difference):
    back_np = np.array(background)
    for y in range(height-1, -1,  -1):
        for x in range(0, width):
            if max(np.abs(np.array(pix[x, y]) - back_np)) > difference:
                return y

def get_x_left(pix, width, height, background, difference):
    back_np = np.array(background)
    for x in range(0, width):
        for y in range(0, height):
            if max(np.abs(np.array(pix[x, y]) - back_np)) > difference:
                return x

def get_x_right(pix, width, height, background, difference):
    back_np = np.array(background)
    for x in range(width-1, -1, -1):
        for y in range(0, height):
            if max(np.abs(np.array(pix[x, y]) - back_np)) > difference:
                return x

img = Image.open('test.jpg')
width, height = img.size
pix = img.load()
background = pix[0,0]

difference = 20 #or whatever works for you here, use trial and error to establish this number
y_top = get_y_top(pix, width, height, background, difference)
y_bot = get_y_bot(pix, width, height, background, difference)
x_left = get_x_left(pix, width, height, background, difference)
x_right = get_x_right(pix, width, height, background, difference)

使用此信息,您可以裁剪图像并保存:

img = img.crop((x_left,y_top,x_right,y_bot))
img.save('test3.jpg')

因此: enter image description here

相关问题 更多 >