sharpen image to detect edges/lines in a stamped X object on paper

2024/10/4 21:22:31

I'm using python & opencv. My goal is to detect "X" shaped pieces in an image taken with a raspberry pi camera. The project is that we have pre-printed tic-tac-toe boards, and must image the board every time a new piece is laid onto the board (with ink stamps). Then the output says what type of piece, if any, is in what section of the tic-tac-toe board.

Here, I have the lines I have detected in the image in green:

As you can see, the "X" shaped pieces seems to not be easily detected. Only one line on some of the stamps gets "seen."

Here's what the edge detection looks like after the filters:

My method for detecting the "X" shaped piece is to check in each section for any lines with a non-horizontal/vertical slope. My problem is that the "X" shaped stamps are not perfect lines; thus, my code hardly picks up on the lines.

I have tried applying an unsharp filter, using histogram equalization, and just using grayscale into edge detection. None of these have detected more than 1 line in any "X" shaped piece.

Roughly what I am doing:

#sharpen image using blur and unsharp method
gaussian_1 = cv2.GaussianBlur(image, (9,9), 10.0)
unsharp_image = cv2.addWeighted(image, 1.5, gaussian_1, -0.5, 0, image)
#apply filter to find stamp pieces, histogram equalization on greyscale
hist_eq = cv2.equalizeHist(unsharp_image)
#edge detection (input,threshold1,threshold2,size_for_sobel_operator)
edges = cv2.Canny(hist_eq,50,150,apertureSize = 3)
#find lines (edges,min_pixels,min_degrees,min_intersections,lineLength,LineGap)
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength,maxLineGap)

Only I'm applying this to each of the 9 sections of the board individually, but that's not really important.

TLDR: How can I make my image so that my lines are "crisp" and sharp? I would like to know what I can use to make a stamped "X" look like a few lines.

Answer

You can try Canny edge detector with Otsu's robust method for determining the dual threshold value.

im = cv2.imread('9WJTNaZ.jpg', 0)
th, bw = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
edges = cv2.Canny(im, th/2, th)

Then you can use

  • convexity defects of the contours

or

  • the area of the filled contour to the area of the bounding box of the contour

to differentiate the cross marks from circles.

This is what I get when I apply Canny to your image.

edges

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

Related Q&A

How to change the color of lines within a subplot?

My goal is to create a time series plot for each column in my data with their corresponding rolling mean. Id like the color of the lines across subplots to be different. For example, for gym and rollin…

Cythons calculations are incorrect

I implemented the Madhava–Leibniz series to calculate pi in Python, and then in Cython to improve the speed. The Python version:from __future__ import division pi = 0 l = 1 x = True while True:if x:pi…

Python: NLTK and TextBlob in french

Im using NLTK and TextBlob to find nouns and noun phrases in a text:from textblob import TextBlob import nltkblob = TextBlob(text) print(blob.noun_phrases) tokenized = nltk.word_tokenize(text) nouns =…

How can I run a script as part of a Travis CI build?

As part of a Python package I have a script myscript.py at the root of my project and setup(scripts=[myscript.py], ...) in my setup.py.Is there an entry I can provide to my .travis.yml that will run my…

Writing nested schema to BigQuery from Dataflow (Python)

I have a Dataflow job to write to BigQuery. It works well for non-nested schema, however fails for the nested schema.Here is my Dataflow pipeline:pipeline_options = PipelineOptions()p = beam.Pipeline(o…

Python decorators on class members fail when decorator mechanism is a class

When creating decorators for use on class methods, Im having trouble when the decorator mechanism is a class rather than a function/closure. When the class form is used, my decorator doesnt get treated…

Why does comparison of a numpy array with a list consume so much memory?

This bit stung me recently. I solved it by removing all comparisons of numpy arrays with lists from the code. But why does the garbage collector miss to collect it?Run this and watch it eat your memor…

StringIO portability between python2 and python3 when capturing stdout

I have written a python package which I have managed to make fully compatible with both python 2.7 and python 3.4, with one exception that is stumping me so far. The package includes a command line scr…

How to redirect data to a getpass like password input?

Im wring a python script for running some command. Some of those commands require user to input password, I did try to input data in their stdin, but it doesnt work, here is two simple python program…

How to grab one random item from a database in Django/postgreSQL?

So i got the database.objects.all() and database.objects.get(name) but how would i got about getting one random item from the database. Im having trouble trying to figure out how to get it ot select on…