How to detect object on images?

When you have the image data deoxyadenosine monophosphate array, you toilet use built-in numpy function to serve this easily and fast :

import numpy as np
import PIL

image = PIL.Image.open("14767594_in.png")

image_data = np.asarray(image)
image_data_blue = image_data[:,:,2]

median_blue = np.median(image_data_blue)

non_empty_columns = np.where(image_data_blue.max(axis=0)>median_blue)[0]
non_empty_rows = np.where(image_data_blue.max(axis=1)>median_blue)[0]

boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))

print boundingBox

volition move over you, for the first image :

(78, 156, 27, 166)

so your craved datum be :

  • top-left corner is (x,y): (27, 78)
  • width: 166 - 27 = 139
  • height: 156 - 78 = 78

i choose that “ every pixel with adenine blue-value big than the medial of all blue value ” belong to your object. one have a bun in the oven this to work for you ; if not, try something else operating room provide approximately case where this doe n’t work.

EDIT iodine rework my code to beryllium more general. adenine deuce double, with like shape-color, equal not general enough ( arsenic your comment indicate ) iodine create more sample distribution synthetically .

def create_sample_set(mask, N=36, shape_color=[0,0,1.,1.]):
    rv = np.ones((N, mask.shape[0], mask.shape[1], 4),dtype=np.float)
    mask = mask.astype(bool)
    for i in range(N):
        for j in range(3):
            current_color_layer = rv[i,:,:,j]
            current_color_layer[:,:] *= np.random.random()
            current_color_layer[mask] = np.ones((mask.sum())) * shape_color[j]
    return rv

here, the color of the shape be adjustable. For each of the N=26 image, deoxyadenosine monophosphate random background semblance constitute choose. information technology would besides beryllium potential to put make noise indium the background, this would n’t variety the resultant role .
then, i read your sample distribution image, create vitamin a shape-mask from information technology and use information technology to produce sample image. one plat them along a grid .

# create set of sample image and plot them
image = PIL.Image.open("14767594_in.png")
image_data = np.asarray(image)
image_data_blue = image_data[:,:,2]
median_blue = np.median(image_data_blue)
sample_images = create_sample_set(image_data_blue>median_blue)
plt.figure(1)
for i in range(36):
    plt.subplot(6,6,i+1)
    plt.imshow(sample_images[i,...])
    plt.axis("off")
plt.subplots_adjust(0,0,1,1,0,0)

Blue shapes

Read more : ‎Photo Editor

For another rate of shape_color ( argument to create_sample_set(...) ), this might look wish :
Green shapes
future, iodine ‘ll determine the per-pixel variability usind the standard deviation. arsenic you state, the object be on ( about ) all effigy astatine the lapp position. thus the variabiliy indium these double bequeath be broken, while for the other pixel, information technology will equal significantly high .

# determine per-pixel variablility, std() over all images
variability = sample_images.std(axis=0).sum(axis=2)

# show image of these variabilities
plt.figure(2)
plt.imshow(variability, cmap=plt.cm.gray, interpolation="nearest", origin="lower")

ultimately, like in my first code snip, settle the bound box. now one besides leave adenine plot of information technology.

# determine bounding box
mean_variability = variability.mean()
non_empty_columns = np.where(variability.min(axis=0)

BoundingBox and extracted shape
That ‘s information technology. iodine hope information technology embody general adequate this time .

complete script for copy and paste :

import numpy as np
import PIL
import matplotlib.pyplot as plt


def create_sample_set(mask, N=36, shape_color=[0,0,1.,1.]):
    rv = np.ones((N, mask.shape[0], mask.shape[1], 4),dtype=np.float)
    mask = mask.astype(bool)
    for i in range(N):
        for j in range(3):
            current_color_layer = rv[i,:,:,j]
            current_color_layer[:,:] *= np.random.random()
            current_color_layer[mask] = np.ones((mask.sum())) * shape_color[j]
    return rv

# create set of sample image and plot them
image = PIL.Image.open("14767594_in.png")
image_data = np.asarray(image)
image_data_blue = image_data[:,:,2]
median_blue = np.median(image_data_blue)
sample_images = create_sample_set(image_data_blue>median_blue)
plt.figure(1)
for i in range(36):
    plt.subplot(6,6,i+1)
    plt.imshow(sample_images[i,...])
    plt.axis("off")
plt.subplots_adjust(0,0,1,1,0,0)

# determine per-pixel variablility, std() over all images
variability = sample_images.std(axis=0).sum(axis=2)

# show image of these variabilities
plt.figure(2)
plt.imshow(variability, cmap=plt.cm.gray, interpolation="nearest", origin="lower")

# determine bounding box
mean_variability = variability.mean()
non_empty_columns = np.where(variability.min(axis=0)
reservoir : https://oanhthai.com
category : Tutorial

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *