how to add data to cvlib - python

Does anyone knows how to add data to cvlib? I want to use
cv.detect_common_objects with my own data.
I was trying to do ant recogniton but cvlib doesn't have any ant data. I have to get answer fast.
code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly
#wcale nie z poradnika :D
img = cv2.imread("ant1.png")
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.axis("off")
plt.imshow(img1)
plt.show()
box, label, count = cv.detect_common_objects(img)
output = draw_bbox(img, box, label, count)
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.axis("off")
plt.imshow(output)
plt.show()

Related

Overlay the template over the reference image using Python

So, I am stuck at a place where I need to superimpose a image in a homographed form to reduce the noise and allow us to reduce the load.
The image where I need to superimpose:
The image which I will be superimposing:
Now, the basketball court has to be superimposed over the 2d drawing, hence allowing me to track the court dynamically over video, thereby get a reference point to track the players inside the court.
Here is the code that I used as of now to track the area
import numpy as np
import cv2
from skimage.io import imread, imshow, imsave
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
from skimage import transform
from skimage.color import rgb2gray
from skimage.feature import match_template
from skimage.feature import peak_local_max
import numpy as np
import glob
import os
from natsort import natsorted
def track_camera(image_path, num, path='./cam_trackings/'):
reference_image = imread("full-court.jpg")
# plt.figure(num=None, figsize=(8, 6), dpi=80)
# imshow(reference_image);
reference_image_gray = rgb2gray(reference_image)
# plt.figure(num=None, figsize=(8, 6), dpi=80)
# imshow(reference_image_gray);
template = rgb2gray(imread(image_path))
# imshow(template);
result = match_template(reference_image_gray, template)
# plt.figure(num=None, figsize=(8, 6), dpi=80)
# imshow(result, cmap='magma');
x, y = np.unravel_index(np.argmax(result), result.shape)
template_width, template_height = template.shape
rect = plt.Rectangle((y, x), template_height, template_width,
color='r', fc='none')
plt.figure(num=None, figsize=(8, 6), dpi=80)
plt.gca().add_patch(rect)
imshow(reference_image_gray);
# imsave(path+'cam_tracking'+str(num)+'.png', reference_image_gray)
plt.savefig(path+'cam_tracking'+str(num)+'.png')
plt.close()
And the output of this is as below (Tracked area):

How to calculate the average grayscale profile of an image?

I would like to calculate the average grayscale profile of an image.
In my next code I have the evolution of the grayscale of all the pixels of the image, but how to make the average? To obtain a single curve, the average of all the others. Thank you
import imageio
import numpy as np
from matplotlib.pyplot import *
from matplotlib import pyplot as plt
img = imread("lion.jpg")
#
red = img[:,:,0]
green = img[:,:,1]
blue = img[:,:,2]
#print(np.mean(img)
line = red[:,:]
#here How to calculate the average grayscale profile?
figure(figsize=(8,4))
plot(line)
plt.show()
If I understand correctly, you want to have a profile of the greyscale image along both directions of the image.
import numpy as np
from matplotlib import pyplot as plt
img = plt.imread("https://i.stack.imgur.com/9qe6z.png")
# convert to grayscale
gray = img.mean(axis=2)
# or
#gray = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
# profile along x -> mean along y
prof_x = gray.mean(axis=0)
# profile along y -> mean along x
prof_y = gray.mean(axis=1)
fig, (ax, ay) = plt.subplots(nrows=2, sharex=True, sharey=True)
ax.plot(prof_x, label="x profile")
ay.plot(prof_y, color="C1", label="y profile")
fig.legend()
plt.show()

Combine picture and plot with matplotlib with alpha channel

I have a .png image with alpha channel and a random pattern generated with numpy.
I want to supperpose both images using matplotlib. The bottom image must be the random pattern and over this, I want to see the second image (attached in the end of the post).
The code for both images is the following:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Random image pattern
fig = plt.subplots(figsize = (20,4))
x = np.arange(0,2000,1)
y = np.arange(0,284,1)
X,Y = np.meshgrid(x,y)
Z = 0.6+0.1*np.random.rand(284,2000)
Z[0,0] = 0
Z[1,1] = 1
# Plot the density map using nearest-neighbor interpolation
plt.pcolormesh(X,Y,Z,cmap = cm.gray)
The result is the following image:
To import the image, I use the following code:
# Sample data
fig = plt.subplots(figsize = (20,4))
# Plot the density map using nearest-neighbor interpolation
plt.imread("good_image_2.png")
plt.imshow(img)
print(img.shape)
The image is the following:
Thus, the final result that I want is:
You can make an image-like array for Z and then just use imshow to display it before the image of the buttons, etc. Note that this only works because your png has an alpha channel.
Code:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Plot the density map using nearest-neighbor interpolation
img = plt.imread("image.png")
(xSize, ySize, cSize) = img.shape
x = np.arange(0,xSize,1)
y = np.arange(0,ySize,1)
X,Y = np.meshgrid(x,y)
Z = 0.6+0.1*np.random.rand(xSize,ySize)
Z[0,0] = 0
Z[1,1] = 1
# We need Z to have red, blue and green channels
# For a greyscale image these are all the same
Z=np.repeat(Z,3).reshape(xSize,ySize,3)
fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
ax.imshow(Z, interpolation=None)
ax.imshow(img, interpolation=None)
fig.savefig('output.png')
Output:
You can also turn off axes if you prefer.
ax.axis('off')

Obtain box2d from superpixels segments

I am trying to use SLIC to obtain superpixels and get semantic segmentation of an image.
img = cv2.imread(img_name)
segments = slic(image, n_segments = numSegments, sigma = 3,convert2lab=True,max_iter=25)
How do I get the box2d for each of the segments? and if there a hierarchical tree of the segments how do I fetch that?
I did not read the original paper, but according to documentation it does not return a hierarchy.
I assume that you mean bounding boxes, so used the skimage example of Regionprops to get bounding boxes for each superpixel returned by SLIC.
Result:
Code:
from skimage.segmentation import slic
from skimage.data import astronaut
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage.measure import label
from skimage.measure import regionprops
from skimage.color import label2rgb
img = astronaut()
segments = slic(img, n_segments=50, compactness = 100)
image_label_overlay = label2rgb(segments, image=img)
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(image_label_overlay)
for region in regionprops(segments):
minr, minc, maxr, maxc = region.bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
plt.show()

How to use Pillow instead cv2?

I have a small histogram program on Python, I want to use Pillow library instead of cv2.
from PIL import Image
import cv2
import matplotlib.pyplot as plt
im = cv2.imread('pic.jpg')
im.ndim == 3:
# Input image is three channels
fig = plt.figure()
fig.add_subplot(311)
plt.hist(im[...,0].flatten(), 256, range=(0, 250), fc='b')
fig.add_subplot(312)
plt.hist(im[...,1].flatten(), 256, range=(0, 250), fc='g')
fig.add_subplot(313)
plt.hist(im[...,2].flatten(), 256, range=(0, 250), fc='r')
plt.show()
I can replace im = cv2.imread('pic.jpg') to im = Image.open('pic.jpg') and im.ndim to im.getbands(), but what can i do with im[...,0].flatten()?
In Python opencv uses numpy arrays as data structures for images. So cv2.imread returns a numpy array.
Matplotlib has a similar function, so for the example in your question you need neither opencv nor pil:
import matplotlib.pyplot as plt
im = plt.imread('pic.jpg')
if im.shape[2] == 3:
# Input image is three channels
fig = plt.figure()
fig.add_subplot(311)
plt.hist(im[...,0].flatten(), 256, range=(0, 250), fc='b')
fig.add_subplot(312)
plt.hist(im[...,1].flatten(), 256, range=(0, 250), fc='g')
fig.add_subplot(313)
plt.hist(im[...,2].flatten(), 256, range=(0, 250), fc='r')
plt.show()
If you have to use PIL to load the image, then you can convert it to a numpy array before plotting:
from PIL import Image
import numpy as np
im = np.array(Image.open('pic.jpg'))
This is how to get pixel values (from 0 to 255) of an image using pillow:
from PIL import Image # import library
import numpy as np # import library
img = Image.open('myImage.png') # use Image.open(image_location)
image_data = np.array(img) # to convert img object to array value use np.array
print(image_data) # now, print all the pixel values of image in np array

Categories

Resources