0

I'm working on image segmentation using caffe, sciki-image, opencv and matplotlib/pyplot. This work involves some image resizing. Essentially, after I've loaded and resized the image:

put_image = caffe.io.load_image(os.path.join(source_dir, pix)
input_image = caffe.io.resize_image(input_image, (600,500), interp_order=3)

I do quite a bit of processing. Everything worked well until I decided to add some contours to the image:

fig, ax = plt.subplots(1, 1)
ax.imshow(image2)
...
ax.fill(vald[:, 0], vald[:, 1], fill=False, color='red', linewidth=1)

where vald is some array and image2 is some output from processed input_image

After saving it

fig.savefig(save_dir + str(idx) + ".png", bbox_inches = 'tight')

I get something like this, which is essentially a cropped version of the original image

enter image description here

So of course my first reaction was to just crop the image and resize it afterwards, but it turned out to lose much of its quality (resolution deteriorated). I tried following suggestions from here and here, but didn't get any improvement either.

Community
  • 1
  • 1
Alex
  • 944
  • 4
  • 15
  • 28
  • Looks like the axis limits have changed. I guess it is putting the image at (0,0) spanning up to (500, 600), but it is actually putting the starting at -100 instead of 0. Try to enforce the `ax.set_xlim([0, 500])` and `ax.set_ylim([0, 600])` (maybe ylim should be 600, 0 due to the inverted y-scale...?) – pathoren Jul 04 '16 at 10:01
  • nice suggestion, but why does the image get rotated? – Alex Jul 04 '16 at 10:13
  • ok fixed it, many thanks! – Alex Jul 04 '16 at 10:31
  • 1
    Great! I post it as an answer to make the question show up as solved. Please accept it if it worked :) – pathoren Jul 06 '16 at 19:07

1 Answers1

1

Follow-up on my comment: The axis limits have changed. The image is in the rectangle between (0,0) and (500, 600). However the x- and y-lim are not exactly that; they range from (-100, -100) up to (500, 600). Just update the limits with ax.set_xlim([0, 500]) and ax.set_ylim([600, 0]) to remove the white frame around.

Note: if you also want to hide the ticks you easily do this with ax.set_xticks([]) and ax.set_yticks([]), or by setting the visibility of the ticks to False.

pathoren
  • 1,634
  • 2
  • 14
  • 22