3

Currently I have an algorithm using Python and OpenCV that detects geometrical shapes and calculates its length (without accuracy).

But I need to make that detection within a specific area, in this case it would be inside a rectangle that I draw with the cv2.rect function but I have no idea how to do it and I couldn't find any information on how to do it on the internet.

I am using python 3.7 and OpenCV 3

Can someone help me?

description of what I want to achieve

2
  • Use only the region of you image covered by your rectangle to further processing. Commented Mar 27, 2019 at 18:28
  • I still do not understand how to do it, I am a beginner in OpenCV. Any function that you recommend? Commented Mar 27, 2019 at 20:02

3 Answers 3

3

Since images are really just arrays, you can directly select a region of interest using slices, like this:

import cv2
frame = cv2.imread(image)

use_this_smaller_frame = frame[50:100, 300:600] 

This will select a region of y ranging from 50 to 100, and x from 300 to 600, giving you a smaller image that is just the part you care about. Note image indexing is [y, x] and not [x, y] as you might expect.

Use the coordinates you use to draw the rectangle to slice the image, then do the rest of your processing on just the smaller image.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems to be [y start:y end, x start:x end] not [x, y] as you said. What I tested: 50 is position from most top to position 50 in vertical, 100 is the same from most top to position 100 in vertical. and 300:600 is horizontal. So, it is [y, x] NOT [x, y].
2

Since you are using Python, you can simply slice the image on the rectangle.

x, y, w, h = cv2.selectROI(window_name, image)  # or something
img_cropped = img[y:y+h, x:x+h] 

You can also check out this tutorial

3 Comments

I need it to be in real time: / ... focusing the Figures only inside the square that processes them ... I'm still investigating
Great answer , you can use img_cropped to apply the motion detection in real time.
Good idea Amine although I found this tutorial that apparently is what I need. thank you medium.com/beyondlabsey/…
0

A simple way to detect motion (naively intended as pixel intensity variation) is by using background subtraction.
Check this OpenCV tutorial. Note that one of the main assumptions here is that the scene background remains 'static'. So every time an object comes or moves in the scene, you can detect it, and for example, track it using a centroid given by the average of the pixels mask.
See Motion Detection: Part 3 - Background Subtraction for a concrete example.

If you want to consider just a part of the image, do as suggested in the other answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.