Image display issue, Neural Style Transfer - python

I am attempting to read in my own images in python following the tensorflow Neural Style Transfer tutorial and when displaying them they look nothing like the original image. Can someone please explain why this is? Is it because of the tensorflow preprocessing? Ultimately my issue with Neural Style Transfer is the style never changes (whole code here). I'm always getting a rainbow style applied, no matter what the style image is, and I'm wondering if this is the issue?
Simple code of how I'm reading in images and displaying for debugging:
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import cv2
style_image = tf.keras.preprocessing.image.img_to_array(Image.open('blackGold.jpg'))
plt.imshow(style_image)
plt.show()
cv2.imshow("img", style_image)
Original Image:
Displaying with matplotlib:
Displaying with opencv:
Neural Style Transfer results:
I appreciate any input, thanks!

Normalize the image, divide the values of your input image, which usually range from 0-255, by 255 so the images are in range of 0-1

Related

Save Numpy array graphs and images as images

I cannot find a way to save my graphs and images. I have tried from
from PIL import Image
im = Image.fromarray()
im.save("your_file.jpeg")
but doesn't work!
Please format your code correctly, it is important for readability and python is intend-sensitive. Also, there is already a post for this problem:
How can I save an image with PIL?

Convert grayscale png to RGB png image

I have a dataset of medical images in grayscale Png format which must be converted to RGB format. Tried many solutions but in vain.
GIMP, Menu image -> Mode -> RGB mode
If you want to just convert the format, the following method will help you:
In python3, using PILLOW and Numpy:
From PIL import Image
import numpy as np
im = Image.open(path/to/image, 'r').convert('L')
im = np.stack((im,)*3, axis=-1)
im = Image.fromarray(im)
im.save(path/to/save)
But if you want to colorize the image, know that colorization is an well-known image translation problem. Even if multiple approachs exist depending on the domain, I don't know any method that colorize any kind of images.
Some ways of doing so is to train a neural network, but for that, you need to have a dataset of B/W and colored images. Here are some approaches:
Using CNNs and considering the colorization as a regression problem: Let there be Color!
Using CNNs and considering the colorization as a classification problem: Colorful Image Colorization
Using GANs : cycle-gan

Scipy imsave darken my image

I'm doing image processing and am working with Python 2.7 in a Jupyter Notebook.
But when I save a numpy array as an image with scipy.misc.imsave(), the result appears darker than when I visualize it with matplotlib.
Here is the result when I plot the image in my notebook :
import matplotlib.pyplot as plot
plot.imshow(img)
And here is the image when I save it :
scipy.misc.imsave(img, 'img.png')
The image appears darker than it should be and I have no idea why. Has someone ever faced a similar problem ?
This is because imsave() normalizes the image between min and max values.
You can do this:
scipy.misc.toimage(img, cmin=0, cmax=255).save('img.png')
It appears that some of my images have values <0, the solution is to clip them between 0 and 255 and the saved image is now correct. But I still don't know why the plot shows them correctly

Detect circle like shapes opencv

everyone i'm fairly new to OpenCV and computer vision and i'm stuck at this problem , which might seem like a fairly trivial but forgive my noobness :)
I'm trying to detect Rebars from a cross-sectional image.
i'm using this code :
import cv2
import cv2.cv as cv
import numpy as np
img = cv2.imread('test/t2.jpg',0)
img = cv2.equalizeHist(img)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
This is the result i'm getting currently, which is not good :
I'm looking for pointers on how to proceed with this problem and how to learn more about CV as i'm really interested!
Thanks a ton!
HoughCircles is not a strong enough way to detect circle in such complex image like your case.
SO has already had some discussion about this. You could refer these post with quality accepted answers
Standard way:
Filled circle detection using CV2 in Python?
What are the possible fast ways to detect circle in an image?
Noise image:
https://dsp.stackexchange.com/questions/5930/find-circle-in-noisy-data
Another method:
Gradient Pair Vectors
Learning Automata
Those results can be slightly improved with setting the parameters better on this line:
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)
For example, you can reduce the maxRadius slightly and increase the sensitivity.
In my experience, however, you won't get a good result on an image like this. It is very complex, the circles are irregular and at different angles. If your goal is to practice, then sure, play with the parameters and try different methods to improve it. I don't see much practical use though.
You can detect features here, using the module trackpy. You need to vary feature sizes with odd numbers and see which one matches best. You may also need to do some pre-processing like, converting image to grayscale.
import trackpy as tp
import numpy as np
import pandas as pd
import pims
import matplotlib.pyplot as plt
#%% importing the data
frames=pims.ImageSequence('F:/TrapHysteresis/processing/Positions/*.TIF')
#%% tracking circles and center positions
featuresize=71
f1=tp.locate(frames[0],featuresize)
plt.figure()
tp.annotate(f1,frames[0])

Some questions about scipy.misc.imshow

I just want to use python and scipy write a program achieve gray-level image histogram equalization, however, I find something is wrong when I use misc.imshow function.
I first read an image by misc.imread function and immediately I write a display function with misc.imshow, the image displayed is not the original one, it seems do the histogram equalization by default. Here is my code:
from sicpy import misc
import matplotlib.pyplot as plt
......
image1 = misc.imread('./images/Fig2.jpg')
misc.imsave('./iamges/post_Fig2.jpg',image1)
plt.figure()
plt.imshow(image1,cmap=plt.cm.gray)
plt.show()
image1 is an image with low contrast.
post_Fig2.jpg is also an image with low contrast
However the image displayed in figure window has a high contrast, significantly different from the two mentioned above.
So I am wondering whether the plt.imshow
function do the histogram equalization automatically? or something is wrong with my code or my method?

Categories

Resources