4

I am working on image to find outer body points but when I save them they have different size and which is creating problem.

  1. My original image is of a person. (1.8Mb)

  2. I create a mask of the person to detect the outer body parts from the original image and save it. (400kb)

  3. From the mask, I obtain the outer body points and plot them on original image, but they not aligned because of difference in size of original and mask image.

    To save images without axes and with full size so that it can match with original image I am saving them by the following method. After saving they look exactly same but due to difference in size points are not aligned.

      plt.axis('off')
      fig.axes.get_xaxis().set_visible(False)
      fig.axes.get_yaxis().set_visible(False)
      plt.savefig('kmask.jpg',bbox_inches='tight',pad_inches = 0,dpi=1500)
    

Result when I plot points on original image:

Result when I plot points on original image

How to deal with such problems?

L_J
  • 2,351
  • 10
  • 23
  • 28
008karan
  • 411
  • 1
  • 3
  • 8
  • Why is the mask a different size than the original image? Are you doing this intentionally? – Djib2011 Aug 03 '18 at 11:25
  • No. When I save by default method It becomes 50kb.Thats why I am changing dpi to create mask of size of original image. – 008karan Aug 03 '18 at 11:32
  • I'm talking about the dimensions of the image, not the memory it requires. A mask always takes up much less memory than an image but usually it has the same dimensions as the original (e.g. 256x256 px) – Djib2011 Aug 03 '18 at 11:36
  • When I remove dpi option while saving the mask then I get very tiny body contour on original body while plotting. – 008karan Aug 03 '18 at 11:43

1 Answers1

0

From what I can tell you are saving the mask in a different size than the original image.

One way to solve this is to first figure out the resolution of the original image. If you don't know you can always check:

img = plt.imread('body_image.jpg')
print(img.shape)
# The first two numbers correspond to the height and width of the image in pixels

The problem is that matplotlib doesn't deal with image resolution the same way. Instead it requires the figure size (in inches) and the DPI (or how many pixels per inch). One way would be to calculate what values you need, and save the image accordingly.

image_height_in_pixels = height_in_inches * dpi

Then use these two numbers to save the mask.

f = plt.figure(figsize=(height_in_inches, width_in_inches))
plt.axis('off')
plt.savefig('kmask.jpg', bbox_inches='tight', pad_inches=0, dpi=dpi)

If this doesn't work try saving the original image too, with matplotlib. This will ensure that both of them have the same dimensions.

Djib2011
  • 6,874
  • 5
  • 36
  • 41