How to calculate Mutual information of 3D images in python - python

I am using simpleITK for image registration, which is a good tool to use. But unfortunately, in the new version, we can not calculate the mutual information value. I have tried other calculation methods, but finally found that it is not applicable to calculate the mutual information of images (such as sklearn package).
Is there a library / tool that already does that? How would you implement it?

Hey it does the trick with SITK
import SimpleITK as sitk
img1 = sitk.ReadImage('')
img2 = sitk.ReadImage('')
registration_method = sitk.ImageRegistrationMethod()
registration_method.SetMetricAsMattesMutualInformation()
registration_method.MetricEvaluate(img1,img2)

Related

How to generate the captcha to train with Python

I would like to use deep learning program for recognizing the captcha using keras with python.
But the big challenge is to generate massive captcha to train.
I want to solve a captcha like this
How can i easily generate above massive captcha to train.
Currently, i use python package captcha
from captcha.image import ImageCaptcha # pip install captcha
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
import os
number = ['0','1','2','3','4','5','6','7','8','9']
MAX_CAPTCHA = 6
WIDTH=100
HEIGHT=30
image = ImageCaptcha(width=WIDTH, height=HEIGHT, font_sizes=[30])
captcha_text = []
for i in range(MAX_CAPTCHA):
c = random.choice(number)
captcha_text.append(c)
#print(captcha_text)
captcha_text = ''.join(captcha_text)
print(captcha_text)
captcha = image.generate(captcha_text)
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
image.write(captcha_text, str(i)+'_'+captcha_text + '.png')
plt.imshow(captcha_image)
plt.show()
If there is no existing similar captcha datasets online I would tackle this problem in the following way:
Get the MNIST dataset
Take one image example and play with it in gimp or some image transformation librari like Open CV, to get a look similar to your captcha examples.
Transform the MNIST examples in a way you find fit (some random noise, and randomized color on black pixels or something)
Train a model on these augmented examples
Now for practical use it depends a little what kind of model you are implementing. If you have a model that can detect and classify all the numbers on the image then you are finished. But if you like to have a simple model that classifies only images with a single digit on them, then you can move a sliding window over your captcha image and only collect outputs of windows for wich the model has high enough confidence that there is some number pressent in the window.
You can generate captchas using PIL python Imaging Library
Another solution may be using Inkscape and its python scripting api.
There are some program already available using these technologies:
https://github.com/kuszaj/claptcha
https://github.com/ramwin/captcha
https://www.quora.com/What-is-the-code-for-generating-an-image-CAPTCHA-using-Python

Python/OpenCV: Computing a depth map from stereo images

I have two stereo images that I'd like to use to compute a depth map. While I unfortunately do not know C/C++, I do know python-- so when I found this tutorial, I was optimistic.
Unfortunately, the tutorial appears to be somewhat out of date. It not only needs to be tweaked to run at all (renaming 'createStereoBM' to 'StereoBM') but when it does run, it doesn't give a good result, even on the example stereo-images that were used in the tutorial itself.
Here's an example:
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('Yeuna9x.png',0)
imgR = cv2.imread('SuXT483.png',0)
stereo = cv2.StereoBM(1, 16, 15)
disparity = stereo.compute(imgL, imgR)
plt.imshow(disparity,'gray')
plt.show()
The result:
This looks very different from what the author of the tutorial achieves:
(source: opencv.org)
Tweaking the parameters does not improve matters. All documentation I've been able to find is for the original C-version of openCV code, not the python-library-equivalent. I unfortunately haven't been able to use this to improve things.
Any help would be appreciated!
You have the images the wrong way around.
Look at the images, the tin behind the lamp lets you work out the camera locations of the two images,
Just change this:
# v
imgR = cv2.imread('Yeuna9x.png',0)
imgL = cv2.imread('SuXT483.png',0)
# ^
If you look at the image in the tutorial which they say is the left frame, it the same as your right one.
Here's my result after the change.
It is possible that you need to keep adjusting the parameters of the block matching algorithm.
have a look at this blog article:https://erget.wordpress.com/2014/03/13/building-an-interactive-gui-with-opencv/
The article's author has composed a set of classes to make the process of calibrating the cameras more streamlined than the opencv tutorial. These classes are available as pypi package: https://github.com/erget/StereoVision
Hope this helps :)
The camera is translated vertically instead of horizontally. Rotate the images 90 degrees, then try. (Prove it to yourself by rotating the screen. I just picked up my laptop and turned it on its edge.)
You mention different software; perhaps a row-major/column-major kind of thing between the original and pyOpenCV.

importing python modules - ImageChops

I'm looking for a good way to analyze image similarity, using python.
I'm NOT looking for a way to establish whether two images are identical. I'm just looking for a way to establish the similarity between two images (e.g., if two images are very similar they could be given a "grade" of 9/10; if they are completely unalike, they'll be given a very low index, such as 2/10).
From some reading that I've done, the module ImageChops has been suggested - however, I haven't been able to find a way to download it.
If anyone knows how to download it, or has a suggestion for other effective solutions, I'd greatly appreciate their advice!
Thanks in advance!
ImageChops is a module from PIL(Pillow). To use the ImageChops function you need to pip install Pillow OR easy_install Pillow OR download the src & extract the src then from CMD CD to the extracted folder & run python setup.py install.
To use the ImageChops you can do this from PIL import ImageChops
you can read the document section
some basic usage example http://effbot.org/imagingbook/imagechops.htm
To check the difference between 2 images:
import Image
from PIL import ImageChops
im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")
diff = ImageChops.difference(im2, im1)
there's a compare images script, but its not a PIL; its on scipy module
You may also check this script here
ImageChops is an module belong to Python Image Library(PIL). Just note that there is no image similarity algorithm (except pixel-wise) built-in in ImageChops, instead, it is a tool you used to write your own algorithm. There is a greate article here:
How can I quantify difference between two images?
The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more.
http://effbot.org/imagingbook/imagechops.htm
you can download the Python Image Library here.
http://www.pythonware.com/products/pil/
There are precompiled package for windows user too.
http://www.lfd.uci.edu/~gohlke/pythonlibs/

how to find the correlation between two images

I need some help in trying to figure out something. I currently a python script which generates two images using the imshow method in matplotlib. My task is to find the correlation between these two images, or in other words the similarity between the two images. Both images are the same size and both use the jet colormap.
Let me know if this is clear enough or if i need to explain in more detail. It would be helpful if someone could provide an example code of how to do this.
Have you looked at the scipy signal processing kit?
from scipy import signal
cor = signal.correlate2d (im1, im2)
will calculate the 2D correlation for you.

How do I find images with a similar color using Python and PIL?

I have a lot of images in a folder, and I would like to find images with a similar color to a pre chosen image.
I would like to be able to do something like:
python find_similar.py sample.jpg
and have that return something like:
234324.jpg
55.jpg
9945.jpg
345434.jpg
104.jpg
Is this doable?
I cannot give you a canned solution, but here's an angle to tackle the problem. It's not PIL-specific, and it might be entirely bogus, since I have no experience in image processing.
Perform color quantization on the image. That gives you a palette that encodes the color information in the image without any shape information.
Run a principal components analysis to get the dominant components in the color cube. Strictly, you could run this without quantization first, but it might be too expensive.
Do a least-squares fitting on the principal components of different images.
Hope this helps.
The algorithm for finding similar images is discussed in a Question on Stackoverflow, you might want to implement one of those in Python & PIL.
Also, you can straightaway use the ImageChops module from PIL and use the difference method to compare two images like this:
import Image
import ImageChops
im1 = Image.open("original.jpg")
im2 = Image.open("sample.jpg")
diff = ImageChops.difference(im2, im1)
That might help you in getting some idea about the difference in your original image and the others.
There is another similar question on Stackoverflow which discusses this.

Categories