How to call up Open CV Durand Tonemap function? - python

I'm trying to examine multiple tone-mapping operators in Open CV, using Python.
Various sources use four operators (Drago, Durand, Reinhard, Mantiuk). Three of them work. However, when I call up cv2.createTonemapDurand(), I get this error:
AttributeError: module 'cv2.cv2' has no attribute 'createTonemapDurand'
Is it possible to call the Durand operator somehow, or did Open CV drop that one recently?
Thanks!

I'll switch from comment to answer to have a better representation.
you just have to :
import cv2
cv2.xphoto.createTonemapDurand()
Be aware that, if u compiled opencv by yourself, you had to check OPENCV_ENABLE_NONFREE.

Please post your code where you import cv2 and call the function. If you want to look for some functions, attributes or whatever either look in the documentation of the package or use dir() and type(). For your example you can use this:
import cv2
from re import match
cv2_filtered = filter(lambda v: match('.*Tonemap', v), dir(cv2))
[print(val) for val in cv2_filtered]
Returns:
Tonemap
TonemapDrago
TonemapMantiuk
TonemapReinhard
createTonemap
createTonemapDrago
createTonemapMantiuk
createTonemapReinhard
Seems like there is no function createTonemapDurand in cv2.

Related

No module named 'sklearn.utils.linear_assignment_'

I am trying to run a project from github , every object counter applications using sort algorithm. I can't run any of them because of a specific error, attaching errors screenshot. Can anyone help me about fixing this issue?
The linear_assignment function is deprecated in 0.21 and will be removed from 0.23, but sklearn.utils.linear_assignment_ can be replaced by scipy.optimize.linear_sum_assignment.
You can use:
from scipy.optimize import linear_sum_assignment as linear_assignment
then you can run the file and don't need to change the code.
pip install scikit-learn==0.22.2
As yiakwy points out in a github comment the scipy.optimize.linear_sum_assignment is not the perfect replacement:
I am concerned that linear_sum_assignment is not equivalent to linear_assignment which later implements "maximum values" matching strategy not "complete matching" strategy, i.e. in tracking problem maybe an old landmark lost and a new detection coming in. We don't have to make a complete assignment, just match as more as possible.
I have found this out while trying to use it inside SORT-based yolo tracking code which that replacement broke (I was lucky that it did otherwise, I would get wrong results from the experiments without realising it...)
Instead, I suggest copying the module itself to the last version of sklearn and include as module in your code.
https://github.com/scikit-learn/scikit-learn/blob/0.22.X/sklearn/utils/linear_assignment_.py
For instance if you copy this file into an utils directory import with from utils.linear_assignment_ import linear_assignment
Solution
Use pip to install lap and optionally scipy
Uncomment the import and use the following function
def linear_assignment(cost_matrix):
try:
import lap
_, x, y = lap.lapjv(cost_matrix, extend_cost=True)
return np.array([[y[i], i] for i in x if i >= 0])
except ImportError:
from scipy.optimize import linear_sum_assignment
x, y = linear_sum_assignment(cost_matrix)
return np.array(list(zip(x, y)))
You are getting this error because you haven't install scikit module yet.
Install scikit-learn module from https://pypi.org/project/scikit-learn/

VSCode Itellisense with python C extension module (petsc4py)

I'm currently using a python module called petsc4py (https://pypi.org/project/petsc4py/). My main issue is that none of the typical intellisense features seems to work with this module.
I'm guessing it might have something to do with it being a C extension module, but I am not sure exactly why this happens. I initially thought that intellisense was unable to look inside ".so" files, but it seems that numpy is able to do this with the array object, which in my case is inside a file called multiarray.cpython-37m-x86_64-linux-gnu (check example below).
Does anyone know why I see this behaviour in the petsc4py module. Is there anything that I (or the developers of petsc4py) can do to get intellisense to work?
Example:
import sys
import petsc4py
petsc4py.init(sys.argv)
from petsc4py import PETSc
x_p = PETSc.Vec().create()
x_p.setSizes(10)
x_p.setFromOptions()
u_p = x_p.duplicate()
import numpy as np
x_n = np.array([1,2,3])
u_n = x_n.copy()
In this example, when trying to work with a Vec object from petsc4py, doing u_p.duplicate() cannot find the function and the suggestion is simply a repetition of the function immediately before. However, using an array from numpy, doing u_n.copy() works perfectly.
If you're compiling in-place then you're bumping up against https://github.com/microsoft/python-language-server/issues/197.

How do I tell which module a function comes from?

I'm trying to read the help on what various things do as I'm reading through code. I'm getting a bit lost in how to determine which module a function comes from. Here is my current example:
import quandl
import numpy as np
import matplotlib.pyplot as plt
amzn = quandl.get("WIKI/AMZN", start_date="2018-01-01", end_date="2019-01-01")
amzn_daily_close = amzn[['Adj. Close']]
amzn_daily_log_returns = np.log(amzn_daily_close.pct_change()+1)
monthly = amzn.resample('BM').apply(lambda x: x[-1])
So given this block of code, I can do help (quandl.get) to see information about that and help (np.log) to see what that does. But when I get to amzn.resample, where is that resample coming from? What should I be entering to see some help information on the resample stuff?
Look at the docstring of quandl.get method to get the help message about the return object. This will contain a statement as returns x-object. Googling about x-object will give you more info on this.
Alternatively, you can do this. To identify what is the object you can do the below.
amzn_type = type(amzn)
This gives the monthly object type. Googling for this type value will give you more insights about that object.Example -
a = 10
print(type(a))
The above code returns <class 'int'> output. Googling about int class in python3 will be helpful.
Inspection
You can 'inspect' the method to find the implementation:
import inspect
print(inspect.getfile(amzn.resample))
# /opt/miniconda/envs/stackoverflow/lib/python3.6/site-packages/pandas/core/generic.py
IDE
Or you can use a good IDE (e.g. PyCharm or IntelliJ) which supports you with some neat features:
Generally, these modules should be documented somewhere. They are usually "packaged" and made available on Python Package Index (pypi). You can search there for your package name and find the quandl page. That may have a link to the projects home page with more documentation.

How to define and use a python function across chunks in R Markdown?

Though I feel very stupid asking this question, I have not found a way to resolve this.
I am trying to define a simple python function in a code chunk in R Markdown.
def show(T):
print("shape of T:", T.shape)
print(T)
Then, I am trying to use it from another pyhton chunk in the same R Markdown.
import numpy as np
T = np.array([[...],[...], ...])
show(T)
But this throws an error saying that
NameError: name 'show' is not defined
I am using RStudio 1.2.907. Is there a way to resolve this?

Auto threshold in ImageJ/FIJI with Python

I'm trying to write python scripts in ImageJ and I'm having problems with autothresholding. It won't let me use the IJ.setAutoThreshold("Default dark"). An example bit of code is below (with a few things left out for clarity):
from ij import IJ, ImagePlus
from java.lang import Runtime, Runnable
import os
for i in filepaths: #filepaths being the files I'm opening
IJ.open(i)
IJ.run("Split Channels") #this is splitting a two channel image
imp = IJ.getImage()
imp.close() #this is closing the channel I don't want
IJ.setAutoThreshold("Default dark") #this is trying to set a threshold
Setting the auto threshold here gives
AttributeError: type object 'ij.IJ' has no attribute 'setAutoTrheshold'
How can I access ImageJ's threshold function?
Cheers!
Have a look at the javadoc: IJ has a method taking two arguments
setAutoThreshold(ImagePlus imp, String method)
so in your case
IJ.setAutoThreshold(imp, "Default dark")
should work.

Categories