OpenCV: How to use the convertScaleAbs() function

匆匆过客 提交于 2021-02-19 06:11:06

问题


I am trying to convert an image back to greyscale 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 greyscale) 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.


回答1:


You can try:

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




回答2:


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)


来源:https://stackoverflow.com/questions/57231962/opencv-how-to-use-the-convertscaleabs-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!