Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: How to use the convertScaleAbs() function

I am trying to convert an image back to grayscale after applying Sobel filtering on it. I have the following code:

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

image = cv2.imread("train.jpg")
img = np.array(image, dtype=np.uint8)

#convert to greyscale
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#remove noise
img_smooth = cv2.GaussianBlur(img_grey, (13,13), 0)

sobely = cv2.Sobel(img_smooth,cv2.CV_64F,0,1,ksize=9)

I want to convert the image sobely back to greyscale using the convertScaleAbs() function.

I know that the function takes a source (the image to be converted to grayscale) and destination array as arguments, but I am not sure what is the best way to go about creating the destination array.

Any insights are appreciated.

like image 290
ceno980 Avatar asked Jul 27 '19 12:07

ceno980


People also ask

What does cv2 ConvertScaleAbs do?

ConvertScaleAbs Method. Scales, computes absolute values and converts the result to 8-bit.

How does OpenCV store images Python?

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2. imwrite() function of opencv python library.

What is cv2 minMaxLoc?

OpenCV cv2. minMaxLoc() is often used to find the maximum and minimum value in a numpy array.


2 Answers

You can try:

gray = cv2.convertScaleAbs(sobely, alpha=255/sobely.max())
plt.imshow(gray, cmap='gray')

like image 86
sim0ne Avatar answered Oct 07 '22 20:10

sim0ne


You can accept the default arguments for the alpha and beta arguments, so the call is simply:

graySobel = cv.convertScaleAbs(sobely)

Then you can call adaptiveThreshold:

thres = cv2.adaptiveThreshold(graySobel, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                          cv2.THRESH_BINARY, 73, 2)
like image 30
Zlatomir Avatar answered Oct 07 '22 21:10

Zlatomir