I have the image as attached. I tried using the below code and it outputs the correct values for most of the images. But however it takes a long time to decode.
import cv2
from pylibdmtx.pylibdmtx import decode
import ctypes
from PIL import Image
decode(Image.open("1591106831_festo.jpg"))
I believe if I can select only the particular section of the image that contains the data matrix and input it to the pylibdmtx library it might be more accurate and faster.
But currently i'm unable to figure out how to select the section of image with data matrix. Could you please help me out. Thanks.
Expected output for the attached DataMatrix is (91)4608
What about simply using cv2 and pylibdmtx as follows
import cv2
from pylibdmtx.pylibdmtx import decode
image = cv2.imread("1591106831_festo.jpg")
h, w = image.shape[:2]
decdd = decode((image[:, :, :1].tobytes(), w, h))
print(decdd)
# [Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]
Which is probably faster that what you currently have in hand since we 1) provide decode with not-to-be-guessed parameters and 2) do image[:, :, :1], which means that one only deals with the first BGR layer, the blue one, clearly sufficient for barcodes.
For the sake of comparison, decode(Image.open("1591106831_festo.jpg")) returns
[Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]
i.e. the exact same inference.
Something you can do is limit the number of barcodes you want to get, using the max_count argument,
>>> decode(Image.open("1591106831_festo.jpg"), max_count=1)
[Decoded(data=b'(91)4608', rect=Rect(left=522, top=629, width=86, height=82))]
Related
I've got a problem. I'm trying create image from binary data which I got from hyperspectral camera. The file which I have is in BSQ uint16 format. From the documentation I found out that images contained in the file (.dat) have a resolution of 1024x1024 and there are 24 images in total. The whole thing is to form a kind of "cube" which I want use in the future to creat multi-layered orthomosaic.
I would also like to add that I am completely new in python but I try to be up to date with everything I need. I hope that everything what I have written is clear and uderstandable.
At first I tried to use Numpy liblary to creating 3D array but ended up with an arrangement of random pixels.
from PIL import Image
import numpy as np
file=open('Sequence 1_000021.dat','rb')
myarray=np.fromfile(file,dtype=np.uint16)
print('Size of new array',":", len(myarray))
con_array=np.reshape(myarray,(24,1024,1024),'C')
naPIL=Image.fromarray(con_array[1,:,:])
naPIL.save('naPIL.tiff')
The result: enter image description here
Example of image which I want to achieve (thumbnail): enter image description here
As suspected it's just byte order, I get a sensible looking image when running the following code in a Jupyter notebook:
import numpy as np
from PIL import Image
# open as big-endian, convert to native order, then reshape as appropriate
raw = np.fromfile(
'./Sequence 1_000021.dat', dtype='>u2'
).astype('uint16').reshape((24, 1024, 1024))
# display inline
Image.fromarray(raw[1,:,:])
I want to convert .nii images to .tif to train my model using U-Net.
1-I looped through all images in the folder.
2-I looped through all slices within each image.
3-I saved each slice as .tif.
The training images are converted successfully. However, the labels (masks) are all saved as black images. I want to successfully convert those masks from .nii to .tif, but I don't know how. I read that it could be something with brightness, but I didn't get the idea clearly, so I couldn't solve the problem until now.
The only reason for this conversion is to be able to train my model. Feel free to suggest a better idea, if anyone can share a way to feed the network with the .nii format directly.
import nibabel as nib
import matplotlib.pyplot as plt
import imageio
import numpy as np
import glob
import os
import nibabel as nib
import numpy as np
from tifffile import imsave
import tifffile as tiff
for filepath in glob.iglob('data/Task04_Hippocampus/labelsTr/*.nii.gz'):
a = nib.load(filepath).get_fdata()
a = a.astype('int8')
base = Path(filepath).stem
base = re.sub('.nii', '', base)
x,y,z = a.shape
for i in range(0,z):
newimage = a[:, :, i]
imageio.imwrite('data/Task04_Hippocampus/masks/'+base+'_'+str(i)+'.tif', newimage)
Unless you absolutely have to use TIFF, I would strongly suggest using the NiFTI format for a number of important reasons:
Image values are often not arbitrary. For example, in CT images the values correspond to x-ray attenuation (check out this Wikipedia page). TIFF, which is likely to scale the values in some way, is not suitable for this.
NIfTI also contains a header which has crucial geometric information needed to correctly interpret the image, such as the resolution, slice thickness, and direction.
You can directly extract a numpy.ndarray from NIfTI images using SimpleITK. Here is a code snippet:
import SimpleITK as sitk
import numpy as np
img = sitk.ReadImage("your_image.nii")
arr = sitk.GetArrayFromImage(img)
slice_0 = arr[0,:,:] # this is a 2D axial slice as a np.ndarray
As an aside: the reason the images where you stored your masks look black is because in NIfTI format labels have a value of 1 (and background is 0). If you directly convert to TIFF, a value of 1 is very close to black when interpreted as an RGB value - another reason to avoid TIFF!
I am using Scikits SSIM to calculate how similar 2 pictures are, and it is working fine for one exception. When there is a lot of white pixels (lets say its a pure white background with a very simple black outlined shape) it will say they are very similar when the actual shape is in fact very different.
I tried looking for other questions about this but couldn't find one that accurately answered my question.
Some code:
from skimage.measure import compare_ssim
import numpy as np
import cv2
# With SSIM, compares image A to image B, and returns the result.
def compare_images(imageA, imageB):
return compare_ssim(imageA, imageB)
# Loads an image with a given filepath with imread.
def load_images(filepath):
picture = cv2.imread(filepath)
# Convert the images to grayscale
return cv2.cvtColor(picture, cv2.COLOR_BGR2GRAY)
# compare the images
original = load_images("images/images.png")
contrast = load_images("images/download.png")
result = compare_images(original, contrast)
print(result)
Mind you, I am just a Python novice. Any help would be welcome.
Trying to process raw DNG pictures in Python with rawpy ends with strange results.
import rawpy
import imageio
from matplotlib import pyplot as plt
path = '/home/stefan/AIJ/RAW.DNG'
with rawpy.imread(path) as raw:
rgb = raw.postprocess()
plt.imshow(rgb)
plt.show()
The result is an rgb picture array with 8-bit values while my camera generates 14 bit raw pictures.
Visualizing the rgb array gives an expected result:
From some googleing I understood that it is possible to import the same file but with an output in 16-bit.
I used the following parameters in the postprocess function:
rgb = raw.postprocess(output_bps=16,demosaic_algorithm=None,output_color = rawpy.ColorSpace.Adobe)
Now the rgb array contains 16 bit values but visualizing results in the following:
Could someone tell me how I could obtain a visualization similar to the first result but handling 16-bit values?
Initially I thought it was related to the fact that my camera is producing 14 bit images rather than 16 bit, but changing the parameter output_bps into 14 gives even worse visualization results.
Thanks in advance!
On request, I would add here the raw picture from a PENTAX K-5 but it is 18MB big and the forum has a limit of 2MB (may be another way to pass you the file?).
I don't think the issue has to do with how you are reading the image, as imshow does not display 16-bit RGB images. So, if you are interested in visually checking the results of reading in the 16-bit image, I would suggest either inspecting bands individually, with
plt.imshow(rgb[:, :, 0])
and so on for each band; or converting the rgb to 8-bit and displaying that, with
rgb8 = (rgb / 256).astype('uint8')
plt.imshow(rgb8)
I have a list called w (size: 784), which I outputted to a png greyscale image:
import matplotlib.pyplot as plt
tmp = 1/(1+np.exp(-10*w/w.max()))
plt.imshow(tmp.reshape(28,28),cmap="gray")
plt.draw()
plt.savefig("final_weight_vector")
Now I want to read the png image back to be a vector.
The solutions I found so far:
First:
import matplotlib.image as mpimg
img=mpimg.imread('final_weight_vector.png')
but img appears to not be greyscale, because its dimensions turend out to be (600, 800, 4).
Second:
reading the file as RGB and converting to greyscale:
im = Image.open('final_weight_vector.png').convert('LA')
However, I couldn't find how to iterate over im so I have no idea as to what's inside. Further, I am not sure the output of im will have the exact same values as the original w.
Help please?
The problem is that what you saved is probably a plot of the 28x28 image, not the image itself.
To be sure, please preview the image. I bet it is 600x800, not 28x28. I also suppose it contains many additional elements, like axes and padding.
If you want to store your array in a loadable format, you may use numpy.save() (and numpy.load() to load it).
You may also use PIL to save your array as image (e.g. using something similar to: http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/)