Home > database >  plt.imshow() display the image inside another image
plt.imshow() display the image inside another image

Time:03-08

I am new to Python and I apologize if this is very basic. I am doing image processing but when I read a png image using cv2 and then display the image using plt.imshow() it gives me strange images(Image with extra boundries). I did the following.

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")

I am attaching the resulting images here.

[the original image]

1

[After displaying the image through plt.imshow().]

2

[after doing opening image processing technique]

3

CodePudding user response:

Is the extra part you're talking about a red area?enter image description here

Output Images:

enter image description here

enter image description here


Of course, if you don't need to use matplotlib you can simply use the cv2.imwrite() to write the images (and cv2.show() to show the images):

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)
  • Related