Wednesday, April 24, 2024
HomePythonA information to discovering books in photographs utilizing Python and OpenCV.

A information to discovering books in photographs utilizing Python and OpenCV.


It is a visitor put up by Adrian Rosebrock from PyImageSearch.com, a weblog all about laptop imaginative and prescient, picture processing, and constructing picture search engines like google and yahoo.

Find books output

I all the time inform those who in the event that they need to study to put in writing nicely they should do two issues:

  1. Follow writing usually.
  2. Learn. So much.

It appears unusual, doesn’t it? How studying usually can dramatically enhance your writing skill.

Nevertheless it’s completely true.

Studying authors that like you may truly engrain their vernacular into your personal writing fashion. And finally, with sufficient apply, you may develop a mode and voice of your personal.

All that mentioned, between the PyImageSearch weblog, my guide Sensible Python and OpenCV, and PyImageSearch Gurus (a pc imaginative and prescient course I’m creating), I write (and skim) lots.

There will likely be moments once I’m actually strolling down the road, espresso in hand, when a stroke of inspiration will strike me like a bolt of lightning.

And fewer I let the fleeting thought disappear into the abyss of my unconscious, I’ve to cease in the course of my stroll, pull out my iPhone, and compose a weblog put up on my tiny display and hard-to-use keyboard.

Is it a bit annoying at occasions? Sure however. Nevertheless it’s a whole lot of enjoyable. Each studying and writing are a ardour of mine – and one ardour fuels the opposite.

So it ought to come as no shock that my espresso desk is roofed in books proper now.

As I’m sitting right here my espresso desk, I made a decision, hey, why not create a Python script to rely the variety of books on my desk? That may be fairly cool, proper? I might merge two of my passions – books and laptop imaginative and prescient.

And when Yasoob invited me to do a visitor put up, I couldn’t assist however settle for. In the remainder of this weblog put up I’ll present you tips on how to create a Python script to rely the variety of books in a picture utilizing OpenCV.

What are we going to do?

Let’s begin by looking at our instance picture we’re going to rely books in:

Example

We see there are 4 books within the picture, together with varied “distractors” resembling a espresso mug, a Starbucks cup, a number of coasters, and a chunk of sweet.

Our purpose right here is to discover the 4 books within the picture whereas ignoring the distractors.

How are we going to try this?

Learn on to search out out!

What libraries will we want?

As a way to construct our system to search out and detect books in photographs, we’ll be using two fundamental libraries:

  • NumPy to facilitate numerical operations.
  • OpenCV to deal with laptop imaginative and prescient and picture processing.

Be sure to have these libraries put in!

Discovering books in photographs utilizing Python and OpenCV.

Let’s go forward and get began.

Open up your favourite code editor, create a brand new file named find_books.py, and let’s get began:

# import the mandatory packages
import numpy as np
import cv2

# load the picture, convert it to grayscale, and blur it
picture = cv2.imread("instance.jpg")
grey = cv2.cvtColor(picture, cv2.COLOR_BGR2GRAY)
grey = cv2.GaussianBlur(grey, (3, 3), 0)
cv2.imshow("Grey", grey)
cv2.waitKey(0)

We’ll begin by importing our required libraries. We’ll be utilizing NumPy for numerical processing and cv2 for our OpenCV bindings.

Loading a picture off disk is dealt with by the cv2.imread perform. Right here we’re merely loading our picture off disk, adopted by changing it from the Purple, Inexperienced, Blue (RGB) coloration house to grayscale.

We’ll additionally blur the picture barely to cut back excessive frequency noise and enhance the accuracy of our code used to search out books later on this put up.

After executing our code, our output ought to appear to be this:

Find books Greyscale

Right here you may see that we have now loaded the picture off disk, transformed it to grayscale, and blurred it barely.

Now, let’s detect edges (i.e outlines) of the objects within the picture:

# detect edges within the picture
edged = cv2.Canny(grey, 10, 250)
cv2.imshow("Edged", edged)
cv2.waitKey(0)

Our edged picture now appears to be like like this:

Find books edged

We’ve got clearly discovered the outlines of the objects within the photographs. Nevertheless, you’ll discover that a number of the outlines aren’t “clear” and full. There are gaps in between the outlines that we have to shut as a way to efficiently detect our books.

To resolve this, we’ll apply a “closing” operation to shut the gaps between the white pixels within the picture:

# assemble and apply a closing kernel to 'shut' gaps between 'white'
# pixels
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.imshow("Closed", closed)
cv2.waitKey(0)

Certain sufficient, the gaps within the outlines have been closed:

Find books Closed

The subsequent step is to really detect the outlines of the objects within the picture. We’ll use the cv2.findContours perform for that:

# discover contours (i.e. the 'outlines') within the picture and initialize the
# whole variety of books discovered
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
whole = 0

Let’s take a second and think about the geometry of a guide.

A guide is a rectangle. And a rectangle has 4 vertices. Subsequently, if we look at a contour and discover that it has 4 vertices, then we are able to assume it’s a guide and never one of many distractors within the picture.

To test if a contour is a guide or not, we have to loop over every of the contours individually:

# loop over the contours
for c in cnts:
  # approximate the contour
  peri = cv2.arcLength(c, True)
  approx = cv2.approxPolyDP(c, 0.02 * peri, True)

  # if the approximated contour has 4 factors, then assume that the
  # contour is a guide -- a guide is a rectangle and thus has 4 vertices
  if len(approx) == 4:
    cv2.drawContours(picture, [approx], -1, (0, 255, 0), 4)
    whole += 1

This code block is the place all of the magic occurs. For every of the contours we compute the perimeter utilizing cv2.arcLength after which approximate the contour utilizing cv2.approxPolyDP.

The rationale we approximate the contour is as a result of the define might not be an ideal rectangle. Because of noise when the picture was captured or shadows within the picture, it’s potential (and even very probably) that the guide is not going to have precisely 4 vertices. By approximating the contour we are able to guarantee we’re capable of side-step this downside.

Lastly, we make a test to see if the approximated contour does certainly have 4 vertices. If it does, then we draw the contour surrounding the guide after which increment the full variety of books counter.

We’ll wrap this instance up by writing the full variety of books discovered to the terminal and displaying the output picture:

# show the output
print "I discovered {0} books in that picture".format(whole)
cv2.imshow("Output", picture)
cv2.waitKey(0)

At this level our output picture ought to appear to be this:

Find books output

And our terminal does certainly present that we have now efficiently discovered the 4 books within the picture will ignoring the opposite distractor objects:

Find books terminal output

To execute the script your self, open up a terminal and execute the next commnd:

$ python find_books.py

Abstract

On this weblog put up you realized tips on how to discover books in photographs utilizing easy picture processing and laptop imaginative and prescient strategies with Python and OpenCV.

In evaluation, our strategy was to:

  1. Load the picture from disk and convert it to grayscale.
  2. Blur the picture barely.
  3. Apply the Canny edge detector to detect edges (i.e. outlines) of the objects within the picture.
  4. Apply a closing morphological operation to shut any gaps within the outlines.
  5. Discover the contours of the objects within the picture.
  6. Apply contour approximation to find out if the contour was a rectangle, and thus a guide.

And that’s all there may be to it!

I hope you loved this weblog put up! And an enormous due to Yasoob for giving me this chance! When you’re desirous about studying extra about myself and laptop imaginative and prescient, take a look at the PyImageSearch weblog.

What are the following steps?

We’re solely scratching the floor of what we are able to do with laptop imaginative and prescient and picture processing. Discovering contours is simply the beginning.

When you’re desirous about studying tips on how to detect faces in photographs, monitor objects in video, or handwriting recognition, check out my guide, Sensible Python and OpenCV. Yasoob has written a evaluation about one in all my programs as nicely.

Downloads:

To obtain the supply code and instance photographs used on this article, use this hyperlink.

Guys I hope that you simply loved this fascinating intro to picture processing in Python and OpenCV. I’m positive that there could be extra posts like this in future. You probably have any feedback and suggestions do remark under.

See you until subsequent time.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments