我是 Python 新手,如果這是非常基本的,我深表歉意。我正在做影像處理,但是當我使用 cv2 讀取 png 影像然后使用 plt.imshow() 顯示影像時,它給了我奇怪的影像(帶有額外邊界的影像)。我做了以下。
import cv2
import numpy as np
img1 = cv2.imread('img.png',cv2.IMREAD_GRAYSCALE)
plt.figure(figsize=(10,4))
plt.imshow(img1, 'gray')
plt.title("ORIGINAL")
plt.savefig("original.png")
kernel = np.ones((4,4),np.uint8)
opening = cv2.morphologyEx(img1, cv2.MORPH_OPEN, kernel)
plt.figure(figsize=(10,4))
plt.imshow(opening, 'gray')
plt.title("OPENING OF ORIGINAL")
plt.savefig("opening of original.png")
我在此處附上生成的影像。
[原圖]

[通過 plt.imshow() 顯示影像后。]

【做開圖處理技術后】

uj5u.com熱心網友回復:
你說的額外部分是紅色區域嗎?
如果我理解正確,此代碼可用于剪切圖片的透明部分
import cv2
import numpy as np
import matplotlib.pyplot as plt
def cv2_crop(im, box):
return im.copy()[box[1]:box[3], box[0]:box[2], :]
def get_transparency_location(image):
height, width, channel = image.shape
assert channel == 4
first_location = None
last_location = None
first_transparency = []
last_transparency = []
for y, rows in enumerate(image):
for x, BGRA in enumerate(rows):
alpha = BGRA[3]
if alpha != 0:
if not first_location or first_location[1] != y:
first_location = (x, y)
first_transparency.append(first_location)
last_location = (x, y)
if last_location:
last_transparency.append(last_location)
top = first_transparency[0]
bottom = first_transparency[-1]
left = None
right = None
for first, last in zip(first_transparency, last_transparency):
if not left:
left = first
if not right:
right = last
if first[0] < left[0]:
left = first
if last[0] > right[0]:
right = last
upper_left = (left[0], top[1])
bottom_right = (right[0], bottom[1])
box =upper_left[0], upper_left[1], bottom_right[0], bottom_right[1]
result = cv2_crop(image, box)
#cv2.imwrite('result.png', result)
return result
if __name__ == '__main__':
#img1 = cv2.imread('img.png',cv2.IMREAD_GRAYSCALE)
image = cv2.imread('img.png', cv2.IMREAD_UNCHANGED)
img1 = get_transparency_location(image)
plt.figure(figsize=(10,4))
plt.imshow(img1, 'gray')
plt.title("ORIGINAL")
plt.savefig("original.png")
kernel = np.ones((4,4),np.uint8)
opening = cv2.morphologyEx(img1, cv2.MORPH_OPEN, kernel)
plt.figure(figsize=(10,4))
plt.imshow(opening, 'gray')
plt.title("OPENING OF ORIGINAL")
plt.savefig("opening of original.png")

uj5u.com熱心網友回復:
以下是如何在不添加任何框架或填充的情況下按原樣保存影像:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img1 = cv2.imread('matches.png', cv2.IMREAD_GRAYSCALE)
kernel = np.ones((4, 4), np.uint8)
opening = cv2.morphologyEx(img1, cv2.MORPH_OPEN, kernel)
fig = plt.figure(frameon=False)
fig.set_size_inches(10, 4)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(img1, cmap='gray', aspect='auto')
fig.savefig("original.png")
ax.imshow(opening, cmap='gray', aspect='auto')
fig.savefig("opening of original.png")
輸入影像:

輸出影像:


當然,如果您不需要使用matplotlib,您可以簡單地使用cv2.imwrite()來撰寫影像(并cv2.show()顯示影像):
import cv2
import numpy as np
img1 = cv2.imread('matches.png', cv2.IMREAD_GRAYSCALE)
kernel = np.ones((4,4),np.uint8)
opening = cv2.morphologyEx(img1, cv2.MORPH_OPEN, kernel)
cv2.imwrite("original.png", img1)
cv2.imwrite("opening of original.png", opening)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440775.html
標籤:Python opencv matplotlib 图像处理
上一篇:如何平滑地繪制移動點
