Python: save binary line masks with assigned line width without using CV2? - python

now I have starting cord(79,143) and end cord(200,100), width 500 and height 500 of an image and I wan to use them to save a binary mask like pic.
I can use the skimage to save it,but the line width seems fixed,and I do want to use cv2, so is there any other solution to save the mask with custom line width?
and meanwhile ,I have a cv2 program, but it does not work,
I have a program:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = np.zeros((1080,1080,3),np.uint8)
for i in range(3):
im=np.squeeze(img[:,:,i])
print(im)
imgg=cv2.line(im,(0,0),(511,511),255,5)
masks=Image.fromarray((imgg).astype(np.uint8))
masks.save("masks"+str(i)+".png")
and I want to save 3 same masks,but it gave error:
Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step1 != elemsize*nchannels)
any idea how to solve it?
Many thanks!
Many thanks!

The OpenCV drawing line function does have a thickness parameter. You can specify it like this:
# Setup an empty image
im = np.zeros((500, 500), dtype=np.uint8)
# Draw with thickness
im = cv2.line(im, (79,143), (200, 100), color=(255, 255, 255), thickness=10)

Related

Create image mask in Python for DNG and processing

I have a RAW image that is saved as .dng from a phone's camera. I want to segment the colors with the OpenCV library in Python. The picture is primarily black and green and I want to get the values of the green parts of the image. I've not worked with images in this way and am completely clueless. The tutorial I am following says to convert the image to H.S.V. color space and to use a mask, but I'm running into problems with the mask, if not in other steps. I'm using Google Colabs.
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
from google.colab import drive
import imageio
import scipy.misc
import skimage.filters
import skimage.metrics
from PIL import Image
# Colabs...
!pip install rawpy
import rawpy
# Colabs...
!pip install ExifRead
import exifread
#image
plate = rawpy.imread('/content/drive/MyDrive/Colab Notebooks/Copy of 0724201706a.dng')
#EXIF
plate_x = open('/content/drive/MyDrive/Colab Notebooks/Copy of 0724201706a.dng', 'rb')
#There are several lines returned. I've left this out for now...
plate_tags = exifread.process_file(plate_x)
plate_tags
plt.imshow(plate.raw_image)
plate_rgb = plate.postprocess( use_camera_wb=True)
plt.imshow(plate_rgb)
plate_rgb.shape
(5312, 2988, 3)
These are a slightly edited RGB, the green channel, and blue channel of the RGB image.
Histograms of the values for each channel in R.G.B. image. The other channels are 0, but green has various values.
I supplied all this info to try to describe the RAW image and the R.G.B.
The tutorial says to convert to the H.S.V. color space. I saw somewhere that the image comes in as B.G.R., so I tried two approaches:
plateRGB_hsv = cv2.cvtColor(plate_rgb, cv2.COLOR_RGB2HSV)
plateBGR_hsv = cv2.cvtColor(plate_rgb, cv2.COLOR_BGR2HSV)
# A lower and upper threshold for mask
hsv_green_lo = (59, 100, 135) #h = 50, s = 100, v = 135)
hsv_green_hi = (75, 250, 255) #h = 75, s = 250, v = 255)
plateRGB_hsv.shape
(5312, 2988, 3)
# Create mask
green_thr = cv2.inRange(plateRGB_hsv, hsv_green_lo, hsv_green_hi)
# Apply mask
img_msk = cv2.bitwise_and(plateRGB_hsv, plateRGB_hsv, green_thr)
plt.subplot(1,2,1)
plt.imshow(green_thr)
plt.subplot(1,2,2)
plt.imshow(img_msk)
plt.show()
Output of the inRange (mask layer creation) and bitwise_and (mask application).
rgb_out = cv2.bitwise_and(plate_rgb, plate_rgb, green_thr)
plt.imshow(rgb_out)
plt.plot()
Apply mask and this is output.
So I didn't seem to create the mask properly? And with the bad mask, there was no change when bitwise_and ran it looks like? I don't know why the mask failed. Is the fact that the R.G.B. or H.S.V. is in three channels complicating the mask and mask application?
The image is here.
EDIT after comments and submitted answer:
I was not clear about what I want my output to look like. I said "green", but really I want it to look like this:
I made a new array with just the green channel as advised.
green_c = plate_rgb[...,1]
But now, I'm confused about how to create a mask. Since the array is just one level, I think of it as a "layer", like in G.I.S. or GIMP, how do I change the unwanted values to black? Sorry if this is obvious. I'm still pretty new to Python.
I am not really sure what you think the problem is. Basically, the Red and Blue channels of your image are empty (look at their mean values in the output below) and you may as well discard them and just use the Green channel as your mask.
#!/usr/bin/env python3
import rawpy
import matplotlib.pyplot as plt
import numpy as np
# Load and process raw DNG
plate = rawpy.imread('raw.dng')
rgb = plate.postprocess()
# Show what we've got
print(f'Dimensions: {rgb.shape}, dtype: {rgb.dtype}')
R = rgb[...,0]
print(f'R channel: min={R.min()}, mean={R.mean()}, max={R.max()}')
G = rgb[...,1]
print(f'G channel: min={G.min()}, mean={G.mean()}, max={G.max()}')
B = rgb[...,2]
print(f'B channel: min={B.min()}, mean={B.mean()}, max={B.max()}')
# Display green channel
plt.imshow(G, cmap='gray')
plt.show()
Output for your image
Dimensions: (5312, 2988, 3), dtype: uint8
R channel: min=0, mean=0.013673103558813567, max=255
G channel: min=0, mean=69.00267554908389, max=255
B channel: min=0, mean=0.017269189710649828, max=255
Keywords: Python, image processing, rawpy, DNG, Adobe DNG format.

How to properly create an blank colored image in numpy (RGBA)

I am trying to create a blank, colored Image in Numpy that includes an alpha channel (RGBA).
In my quest to find out how to do this, I came to this SO answer. However, when I run either example of the SO answer, the red appears blue, unlike the images shown in the answer.
So, I am wondering what the correct way to do this is as uint16 (instead of uint8) and without the color mix-up? Is this a bug with Numpy or am I doing something wrong?
My resulting image:
https://i.ibb.co/VVxhDwY/img.png
My code:
import cv2
import numpy as np
size = (128, 256)
blank_image = np.zeros((size[0], size[1], 4), np.uint8)
# Make first 10 rows red and opaque
blank_image[:10] = [255,0,0,255]
# Make first 10 columns green and opaque
blank_image[:,:10] = [0,255,0,255]
cv2.imwrite('test.png', blank_image)
The answer you link to was using PIL/Pillow (which uses conventional RGB ordering) rather than OpenCV (which uses BGR ordering). If you want a semi-transparent solid red in 16-bit with OpenCV, use:
#!/usr/bin/env python3
import cv2
import numpy as np
size = (128, 256)
# Make semi-transparent solid red image
im = np.zeros((*size, 4), np.uint16) + [0,0,65535,128]
# Save to disk
cv2.imwrite('result.png', im)
If instead of cv2 you use matplotlib.pyplot, your code runs perfectly fine (see below) and then you can simply store it with this library already.

How to represent a binary image as a graph with the axis being height and width dimensions and the data being the pixels

I am trying to use Python along with opencv, numpy and matplotlib to do some computer vision for a robot which will use a railing to navigate. I am currently extremely stuck have run out of places to look. My current code is:
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('railings.jpg')
railing_image = np.copy(image)
resized_image = cv2.resize(railing_image,(881,565))
gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 85, 255)
cv2.imshow('test',canny)
image_array = np.array(canny)
ncols, nrows = image_array.shape
count = 0
scan = np.array
for x in range(0,image_array.shape[1]):
for y in range(0,image_array.shape[0]):
if image_array[y, x] == 0:
count += 1
scan = [scan, count]
print(scan)
plt.plot([0, count])
plt.axis([0, nrows, 0, ncols])
plt.show()
cv2.waitKey(0)
I am using a canny image which is stored in an array of 1's and 0's, the image I need represented is
The final result should look something like the following image.
I've tried using a histogram function but I've only managed to get that to output essentially a count of the number of times a 1 or 0 appears.
If anyone could help me or point me in the right direction that would produce a graph that represents the image pixels within a graph of height and width dimensions.
Thank you
I'm not sure how general this is but you could just use numpy argmax to get location of the maximum (like this) in your case. You should avoid loops as this will be very slow, better to use numpy functions. I've imported your image and used the cutoff criterion that 200 or more in the yellow channel is railing,
import cv2
import numpy as np
import matplotlib.pyplot as plt
#This loads the canny image you uploaded
image = cv2.imread('uojHJ.jpg')
#Trim off the top taskbar
trimimage = image[100:, :,0]
#Use argmax with 200 cutoff colour in one channel
maxindex = np.argmax(trimimage[:,:]>200, axis=0)
#Plot graph
plt.plot(trimimage.shape[0] - maxindex)
plt.show()
Where this looks as follows:

OpenCV Python: Normalize image

I'm new to OpenCV. I want to do some preprocessing related to normalization. I want to normalize my image to a certain size. The result of the following code gives me a black image. Can someone point me to what exactly am I doing wrong? The image I am inputting is a black/white image
import cv2 as cv
import numpy as np
img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
cv.normalize(img, normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', self.normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()
as one can see at: http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#cv2.normalize, there is a → dst that say that the result of the normalize function is returned as output parameter. The function doesn't change the input parameter dst in-place.
(The self. in cv.imshow('dst_rt', self.normalizedImg) line is a typo)
import cv2 as cv
import numpy as np
path = r"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"
img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
normalizedImg = cv.normalize(img, normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()
It's giving you a black image because you are probably using different sizes in img and normalizedImg.
import cv2 as cv
img = cv.imread(path)
img = cv.resize(img, (800, 800))
cv.normalize(img, img, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', img)
cv.waitKey(0)
cv.destroyAllWindows()
Update: In NumPy there are more intuitive ways to do this ref:
a = np.random.rand(3,2)
# Normalised [0,1]
b = (a - np.min(a))/np.ptp(a)
# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
c = (255*(a - np.min(a))/np.ptp(a)).astype(int)
# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1
When you call cv.imshow() you use self.normalizedImg, instead of normalizedImg.
The self. is used to identify class members and its use in the code you've written is not appropriate. It shouldn't even run as written. However I assume this code has been extracted from a class definition, but you must be consistent in naming variables and self.normalizedImg is different from normalizedImg.

crop image in skimage?

I'm using skimage to crop a rectangle in a given image, now I have (x1,y1,x2,y2) as the rectangle coordinates, then I had loaded the image
image = skimage.io.imread(filename)
cropped = image(x1,y1,x2,y2)
However this is the wrong way to crop the image, how would I do it in the right way in skimage
This seems a simple syntax error.
Well, in Matlab you can use _'parentheses'_ to extract a pixel or an image region. But in Python, and numpy.ndarray you should use the brackets to slice a region of your image, besides in this code you is using the wrong way to cut a rectangle.
The right way to cut is using the : operator.
Thus,
from skimage import io
image = io.imread(filename)
cropped = image[x1:x2,y1:y2]
One could use skimage.util.crop() function too, as shown in the following code:
import numpy as np
from skimage.io import imread
from skimage.util import crop
import matplotlib.pylab as plt
A = imread('lena.jpg')
# crop_width{sequence, int}: Number of values to remove from the edges of each axis.
# ((before_1, after_1), … (before_N, after_N)) specifies unique crop widths at the
# start and end of each axis. ((before, after),) specifies a fixed start and end
# crop for every axis. (n,) or n for integer n is a shortcut for before = after = n
# for all axes.
B = crop(A, ((50, 100), (50, 50), (0,0)), copy=False)
print(A.shape, B.shape)
# (220, 220, 3) (70, 120, 3)
plt.figure(figsize=(20,10))
plt.subplot(121), plt.imshow(A), plt.axis('off')
plt.subplot(122), plt.imshow(B), plt.axis('off')
plt.show()
with the following output (with original and cropped image):
You can crop image using skimage just by slicing the image array like below:
image = image_name[y1:y2, x1:x2]
Example Code :
from skimage import io
import matplotlib.pyplot as plt
image = io.imread(image_path)
cropped_image = image[y1:y2, x1:x2]
plt.imshow(cropped_image)
you can go ahead with the Image module of the PIL library
from PIL import Image
im = Image.open("image.png")
im = im.crop((0, 50, 777, 686))
im.show()

Categories

Resources