How to use the convertScaleAbs() function in OpenCV?

2024/10/5 5:11:37

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 cv2image = 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.

Answer

You can try:

gray = cv2.convertScaleAbs(sobely, alpha=255/sobely.max())
plt.imshow(gray, cmap='gray')
https://en.xdnf.cn/q/70525.html

Related Q&A

Register a Hello World DBus service, object and method using Python

Im trying to export a DBus service named com.example.HelloWorld, with an object /com/example/HelloWorld, and method com.example.HelloWorld.SayHello that prints "hello, world" if the method i…

Python 3 Timedelta OverflowError

I have a large database that I am loading into an in-memory cache. I have a process that does this iterating through the data day by day. Recently this process has started throwing the following error:…

Constructing hierarchy from dictionary/JSON

Im looking for a way to create hierarchy in form of child parent relationship between two or more instances of same class.How would one go about creating such objects from nested dictionary like in exa…

PyPy file append mode

I have code like this:f1 = open(file1, a) f2 = open(file1, a)f1.write(Test line 1\n) f2.write(Test line 2\n) f1.write(Test line 3\n) f2.write(Test line 4\n)When this code is run with standard Python 2.…

Clean up ugly WYSIWYG HTML code? Python or *nix utility

Im finally upgrading (rewriting ;) ) my first Django app, but I am migrating all the content. I foolishly gave users a full WYSIWYG editor for certain tasks, the HTML code produced is of course terribl…

Using config files written in Python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Model not defined when using foreign key to second model

Im trying to create several relationships between some models such as User and Country. When I try to syncdb, my console outputs "Name Country is not defined". Here is the code: class User(mo…

Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

Im trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. It works fine if I just send the POST request through a local python script.What I want to do is create a …

django deploy to Heroku : Server Error(500)

I am trying to deploy my app to heroku. Deploy was done correctly but I got Server Error(500). When I turned DEBUG true, Sever Error doesnt occurred. So I think there is something wrong with loading st…

Should I preallocate a numpy array?

I have a class and its method. The method repeats many times during execution. This method uses a numpy array as a temporary buffer. I dont need to store values inside the buffer between method calls. …