0

I am trying to use openCV in order to attain a black and white image of a picture, coloring in highly gradient pixels in white, and low gradient with black. I am currently using anaconda spyder and have been able to create the new image, but the image resolution is significantly lower than the image that I have supplied. Does anyone know how to resize an image produced by plt.imshow?

My code is as follows, and I have attached a picture.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) #grayscale conversion
plt.imshow(gray, cmap='gray')

kernel_size = 3
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
lowthresh = 125
highthresh = 150
edges = cv2.Canny(blur_gray, lowthresh, highthresh)

plt.imshow(edges, cmap = 'Greys_r', aspect = 'auto')

output of my code

picture to take gradient of

2
  • You are viewing it inside Jupyter Notebook I guess, So the displays the image in a fixed width/height which suits the UI, However if you see the scale on the plot, you would notice that the image still has dimensions of 800x500 something, You may try to write the image to a location using cv2.imwrite() Commented Mar 19, 2017 at 5:18
  • PERFECT! I have been struggling to get this to output how I wanted to, I've gotta start asking more questions on here! Thank you. Commented Mar 19, 2017 at 5:45

1 Answer 1

5

You can define the figure size prior to plotting with the figsize attribute when defining a figure: plt.figure(figsize=(20, 4)).

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great solution as well! Now I can see the image plotted with axis'!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.