How to I extract objects? [closed]

2024/11/20 15:19:23

I want to split images like this in a way that every symbols gets splits up vertically kind of like this input image:

![input image][1]

to this:

![here][2]

The problem is each symbol might have different width so I can't really fix the splitting points like we do in array splitting. If all objects had same width then I could segment the image base on width. In this scenario, what logic I should use to extract these connected objects?

Answer

First load the img from the url

import numpy as np
import urllib.request
from PIL import Image
from matplotlib import pyplot as plturllib.request.urlretrieve('https://i.sstatic.net/GRHzg.png',"img.png")
img = Image.open("img.png")
img.show()

enter image description here

Then consider the black part as "filled" and convert in numpy array

arr = (np.array(img)[:,:,:-1].sum(axis=-1)==0)

If we sum the rows values for each column we can have a simple sum of how much pixel are filled in each column:

plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(arr.sum(axis=0))
plt.xlim(0,arr.shape[1])

enter image description here

finally if we compute the differential of this sum over the columns we can obtain the following result:

plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(np.diff(arr.sum(axis=0)))
plt.xlim(0,arr.shape[1])

enter image description here

At this point you can simply chose a threshold and cut the image:

threshold = 25
cut = np.abs(np.diff(arr.sum(axis=0)))>threshold
x_lines = np.arange(len(cut))[cut]plt.imshow(arr, aspect="auto")
plt.vlines(x_lines, 0, arr.shape[0], color="r")

enter image description here

This is my solution and it works fine, but it is sensitive to the chosen threshold and to the columns gradient. I hope it is useful.

https://en.xdnf.cn/q/119812.html

Related Q&A

sklearn.metrics.roc_curve only shows 5 fprs, tprs, thresholds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 2 years ago.Improve…

I can not transform a file to a dictionary in python [duplicate]

This question already has answers here:ValueError: need more than 1 value to unpack python(4 answers)Closed 5 years ago.I am trying to transform a file to dictionary but having error.def txt_to_dict():…

Loan payment calculation

I am learning Python and am stuck. I am trying to find the loan payment amount. I currently have:def myMonthlyPayment(Principal, annual_r, n):years = nr = ( annual_r / 100 ) / 12MonthlyPayment = (Princ…

How can I implement this model?

Problem statement I have 3 classes (A, B, and C). I have 6 features: train_x = [[ 6.442 6.338 7.027 8.789 10.009 12.566][ 6.338 7.027 5.338 10.009 8.122 11.217][ 7.027 5.338 5.335 8.122 5.537…

How do I change a variable inside a variable?

Heres my code :hp1 = 100 health1 = you have, hp1hp1 = hp1 - 50 health1print hp1 print health1This is what it prints :50 (you have, 100)Why doesnt the hp1 change inside the health?

Why do I get NameError: name ... is not defined in python module?

filename:recom.py# Returns a distance-based similarity score for person1 and person2 def sim_distance(prefs,person1,person2): # Get the list of shared_itemssi={}for item in prefs[person1]:if item in pr…

How to form boxes from nearly touching lines

I want to detect corners from a image with boxes, although i created the chessboard edge lines with the EDlines algorithm. Now, I have some problems to join them to create perfect boxes. Could you help…

How to change app.py variable with HTML button?

How do I add or subtract 1 from the variable num ( in flask route) with an HTML button? So when I click the button it change the var to 1 and refresh the page to show the new value @app.route(/) def n…

How to check if time is in the range between two days?

I found some nice examples to check, if a time is in a specific range, like this one:now_time = datetime.datetime.now().time() start = datetime.time(17, 30) end = datetime.time(4, 00) if start <=…

Removing Duplicate Domain URLs From the Text File Using Bash

Text file https://www.google.com/1/ https://www.google.com/2/ https://www.google.com https://www.bing.com https://www.bing.com/2/ https://www.bing.com/3/Expected Output: https://www.google.com/1/ https…