plt.imshow() 在另一个图像中显示图像
plt.imshow() display the image inside another image
我是 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()显示图像后。]
[做开图处理技术后]
你说的多余部分是红色区域吗?
如果我没理解错的话,这段代码可以用来切掉图片的透明部分
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")
以下是如何按原样保存图像,不添加任何边框或填充:
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()
to write the images (and 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)
我是 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()显示图像后。]
[做开图处理技术后]
你说的多余部分是红色区域吗?
如果我没理解错的话,这段代码可以用来切掉图片的透明部分
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")
以下是如何按原样保存图像,不添加任何边框或填充:
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()
to write the images (and 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)