我掃描了各種產品包裝的影像如下

這些有不同的尺寸。我需要洗掉右側和下方的白色背景。我嘗試了幾種解決方案,但只有
uj5u.com熱心網友回復:
我沒有使用“opencv”庫,我使用了“skimage”。去掉白色背景后,圖片的解析度受到了影響,不知道大家能不能接受。這是代碼:
from skimage import io
def corp_margin(img, threshold=600):
img2=img.sum(axis=2)
(row,col)=img2.shape
row_top=0
raw_down=0
col_top=0
col_down=0
for r in range(0,row):
if img2.sum(axis=1)[r]<threshold*col:
row_top=r
break
for r in range(row-1,0,-1):
if img2.sum(axis=1)[r]<threshold*col:
raw_down=r
break
for c in range(0,col):
if img2.sum(axis=0)[c]<threshold*row:
col_top=c
break
for c in range(col-1,0,-1):
if img2.sum(axis=0)[c]<threshold*row:
col_down=c
break
new_img=img[row_top:raw_down 1,col_top:col_down 1,0:3]
return new_img
image = io.imread("img.jpg")
img_re = corp_margin(image)
io.imshow(img_re)
io.imsave('new_image.jpg', img_re)
這是我得到的結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505694.html
上一篇:嘗試解決此控制流問題時遇到問題
