How can i find center coordinates of a group object opencv - image

I want to center x,y coordinates of all delight lokums in this picture;
marshmallows on a tray
i tried a lot of things but i cant complete rectangles.
i get this image
enter image description here
target my project
project target
import cv2
import numpy as np
img = cv2.imread ('yl4.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#morphology
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(gray,kernel,iterations = 1)
kernel = np.ones((17,17),np.uint8)
opening = cv2.morphologyEx(gray , cv2.MORPH_OPEN, kernel)
#get binary
gray_blur = cv2.GaussianBlur(opening, (21,21), 0)
thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, 11, 1)
kernel = np.ones((3,3),np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = np.ones((7,7),np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
#filter small dotted regions
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh , None, None, None, 4, cv2.CV_32S)
sizes = stats[1:, -1] #get CC_STAT_AREA component
img2 = np.zeros((labels.shape), np.uint8)
for i in range(0, nlabels - 1):
if sizes[i] >= 275:
img2[labels == i + 1] = 255
cv2.imshow('frame',img2)
cv2.imshow('gray',gray)
cv2.imwrite("outyl3.jpg", img2)
cv2.waitKey(0)

Related

Particle segmentation from background image

Here in this image, all the black strands and small black dots you can see are particles (foreground) and the white, yellowish, greenish, grayish, and small bluish areas are the backgrounds. I am looking for an algorithm to extract the foreground and replace the background with some known color say: white. Can you please suggest to me a better solution to achieve the target?
Thank you
Simple color thresholding should work here. The idea is to isolate black by using HSV color thresholding with a lower/upper threshold range to obtain a mask then cv2.bitwise_and to get the filtered result
You didn't specify a language so here's an implementation in Python
Code
import cv2
import numpy as np
image = cv2.imread('1.jpg')
# Set minimum and maximum HSV values to display
lower = np.array([0,0,0])
upper = np.array([179,255,52])
# Convert to HSV format and color threshold
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(image, image, mask=mask)
result[mask==0] = (255,255,255)
# Display
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
# cv2.imwrite('result.png', result)
cv2.waitKey()
HSV color thresholder script with sliders, remember to change the image file path. You can play with the sliders to refine your segmentation.
import cv2
import numpy as np
def nothing(x):
pass
# Load image
image = cv2.imread('1.jpg')
# Create a window
cv2.namedWindow('image')
# Create trackbars for color change
# Hue is from 0-179 for Opencv
cv2.createTrackbar('HMin', 'image', 0, 179, nothing)
cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
cv2.createTrackbar('VMax', 'image', 0, 255, nothing)
# Set default value for Max HSV trackbars
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize HSV min/max values
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
while(1):
# Get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin', 'image')
sMin = cv2.getTrackbarPos('SMin', 'image')
vMin = cv2.getTrackbarPos('VMin', 'image')
hMax = cv2.getTrackbarPos('HMax', 'image')
sMax = cv2.getTrackbarPos('SMax', 'image')
vMax = cv2.getTrackbarPos('VMax', 'image')
# Set minimum and maximum HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Convert to HSV format and color threshold
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(image, image, mask=mask)
result[mask==0] = (255,255,255)
# Print if there is a change in HSV value
if((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display result image
cv2.imshow('image', result)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()

PIL.ImageDraw.ImageDraw.text features attribute in Pillow 7.0.0 dosen't seem to give any difference in results

This is the code
img = np.full(shape=(40, 225, 3), fill_value=211, dtype=np.uint8)
b,g,r,a = 0,0,0,0
fontpath = "arial.ttf"
font = ImageFont.truetype(fontpath, 14)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
draw.text((25, 10), captcha, font=font, features=['cpsp', 'dist'], fill=(b, g, r, a))
# w=img_pil.rotate(17.5, expand=1)
# img_pil = Image.paste( ImageOps.colorize(w, (0,0,0), (255,255,84)), (242,60), w)
img = np.array(img_pil)
noise_factor = np.random.uniform(low=0.4, high=0.8, size=1)
gauss = np.random.normal(0, noise_factor, img.size)
gauss = gauss.reshape(img.shape[0],img.shape[1],img.shape[2]).astype('uint8')
noise = img + img * gauss
## Display
gray = cv2.cvtColor(noise, cv2.COLOR_BGR2GRAY)
cv2.imwrite(captcha+".png", gray)
The above code didn't alter the space between the characters, am I using it right?
Please include some examples in https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html on how to use this.
Click Here to see the output for the above code

Remove only black dot in the image using opencv

I am posting to understand that is that possible to remove only black dot in the image.
Here are two methods:
Method #1: Contour filtering
We convert the image to grayscale, Otsu's threshold for a binary image, then find contours and filter using a minimum threshold area. We remove the black dots by drawing filling in the contours to effectively erase the dots
import cv2
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
if cv2.contourArea(c) < 10:
cv2.drawContours(thresh, [c], -1, (0,0,0), -1)
result = 255 - thresh
cv2.imshow('result', result)
cv2.waitKey()
Method #2: Morphological operations
Similarly, we convert to grayscale then Otsu's threshold. From here we create a kernel and perform morph open
import cv2
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = 255 - cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
cv2.imshow('opening', opening)
cv2.waitKey()

matplotlib: jpg image special effect/transformation as if its on a page of an open book

I wish to appear a figure (and certain text) as if they are printed on a page of an open book. Is it possible to transform an jpg image programmatically or in matplotlib to have such an effect?
You can use a background axis along with an open source book image to do something like this,
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax2 = fig.add_axes([0.2, 0.3, 0.25, 0.3])
#Plot page from a book
im = plt.imread("./book_page.jpg")
implot = ax1.imshow(im, origin='lower')
# Plot a graph and set background to transparent
x = np.linspace(0,4.*np.pi,40)
y = np.sin(x)
ax2.plot(x,y,'-ro',alpha=0.5)
ax2.set_ylim([-1.1,1.1])
ax2.patch.set_alpha(0.0)
from matplotlib import rc
rc('text', usetex=True)
margin = im.shape[0]*0.075
ytext = im.shape[1]/2.+10
ax1.text(margin, ytext, "The following text is an example")
ax1.text(margin, 90, "Figure 1. Showing a sine function")
plt.show()
Which looks like this,
where I used the following book image.
UPDATE: Added non-affine transformation based on scikit-image warp example, but with Maxwell distribution. The solution saves the matplotlib line as an image in order to apply a pointwise transform. Mapping for vector graphics may be possible but I think this will be more complicated...
import numpy as np
import matplotlib.pyplot as plt
def maxwellian_transform_image(image):
from skimage.transform import PiecewiseAffineTransform, warp
rows, cols = image.shape[0], image.shape[1]
src_cols = np.linspace(0, cols, 20)
src_rows = np.linspace(0, rows, 10)
src_rows, src_cols = np.meshgrid(src_rows, src_cols)
src = np.dstack([src_cols.flat, src_rows.flat])[0]
# add maxwellian to row coordinates
x = np.linspace(0, 3., src.shape[0])
dst_rows = src[:, 1] + (np.sqrt(2/np.pi)*x**2 * np.exp(-x**2/2)) * 50
dst_cols = src[:, 0]
dst_rows *= 1.5
dst_rows -= 1.0 * 50
dst = np.vstack([dst_cols, dst_rows]).T
tform = PiecewiseAffineTransform()
tform.estimate(src, dst)
out_rows = image.shape[0] - 1.5 * 50
out_cols = cols
out = warp(image, tform, output_shape=(out_rows, out_cols))
return out
#Create the new figure
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
#Plot page from a book
im = plt.imread("./book_page.jpg")
implot = ax.imshow(im, origin='lower')
# Plot and save graph as image, will need some manipulation of location
temp, at = plt.subplots()
margin = im.shape[0]*0.1
x = np.linspace(margin,im.shape[0]/2.,40)
y = im.shape[1]/3. + 0.1*im.shape[1]*np.sin(12.*np.pi*x/im.shape[0])
at.plot(x,y,'-ro',alpha=0.5)
temp.savefig("lineplot.png",transparent=True)
#Read in plot as an image and apply transform
plot = plt.imread("./lineplot.png")
out = maxwellian_transform_image(plot)
ax.imshow(out, extent=[0,im.shape[1],0,im.shape[0]])
plt.show()
The figure now looks like,

Detecting rectangle in image

I would like to ask if somebody could help me with rectangle detection in image.
I've tried contour detection, but results are not sufficient.
I'd be thankful for any ideas.
I have tried this:
EDIT 1
# loading image
im = cv2.imread('b.jpeg',cv2.IMREAD_GRAYSCALE)
# equalization
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
im = clahe.apply(im)
# bluring image
im = cv2.GaussianBlur(im,(5,5),0)
# edge detection
edges = cv2.Canny(im,100,200)
cv2.imshow('Canny',edges)
cv2.moveWindow('Canny',0,0)
cv2.waitKey(0)
cv2.destroyAllWindows()
# contour extraction
ret,thresh = cv2.threshold(edges,50,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imshow('Contour',im)
cv2.moveWindow('Contour',0,0)
cv2.waitKey(0)
cv2.destroyAllWindows()
cntrRect = []
for i in contours:
approx = cv2.approxPolyDP(i,0.1*cv2.arcLength(i,True),True)
x,y,w,h = cv2.boundingRect(approx)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
#peri = cv2.arcLength(i,True)
epsilon = 0.1*cv2.arcLength(i,True)
#0.02*peri
approx = cv2.approxPolyDP(i,epsilon,True)
if len(approx) == 4:
cntrRect.append(approx)
cv2.imshow('Contour2',im)
cv2.moveWindow('Contour2',0,0)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.drawContours(im, cntrRect, -1, (255,0,0), 3)
cv2.imshow('Contour3',im)
cv2.moveWindow('Contour3',0,0)
cv2.waitKey(0)
cv2.destroyAllWindows()

Resources