Python: OpenCV - Selecting Region of an Image
You are correct about the ROI-Formular which is [y:y+h, x:x+w]
, however the initial point on y
coordinate is wrong that is why you are cropping the white region of the image.
You are probably looking for:
dmc = im[13:13+287, 938:938+287]
cv2.imshow('dmc', dmc)
Result:
OpenCV: return only selected area of an image and return the rest as black
The issue has been solved by creating masks and combine the foreground and background by these lines of code:
import cv2
import numpy as np
pic= cv2.imread('Assets/set.jpeg')
pic = cv2.resize(pic, dsize=(500, 400), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),5)
_,thres = cv2.threshold(blur, 100,250, cv2.THRESH_TOZERO)
res = cv2.Canny(thres, 100, 250, L2gradient=True)
circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=15,minRadius=80,maxRadius=100)
circles = np.uint16(np.around(circles))
mask = np.full((res.shape[0], res.shape[1]), 1, dtype=np.uint8) # mask is only
clone = pic.copy()
for i in circles[0, :]:
cv2.circle(mask, (i[0], i[1]), i[2], (255, 255, 255), -1)
cv2.circle(clone, (i[0], i[1]), i[2], (255, 255, 255), 1)
# get first masked value (foreground)
fg = cv2.bitwise_or(res, res, mask=mask)
# get second masked value (background) mask must be inverted
mask = cv2.bitwise_not(mask)
background = np.full(res.shape, 255, dtype=np.uint8)
bk = cv2.bitwise_or(background, background, mask=mask)
# combine foreground+background
final = cv2.bitwise_or(fg, bk)
result = np.concatenate((res,final),axis=1)
cv2.imshow('Hole',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Nothing to be asked anymore and I will close the question. Thank you!!
How to select region on image and extract pixel values in numbers of that particular region?
The function to use is cv.selectROI
It can take different arguments. The simplest case is just taking an image (numpy array). Here's an example where also the window name is specified.
Personally I also prefer to use the fromCenter
argument.
import numpy as np
import cv2 as cv
im = cv.imread(cv.samples.findFile("lena.jpg"))
bbox = cv.selectROI("lena", im, fromCenter=True)
cv.destroyWindow("lena")
print("region:", bbox)
(x,y,w,h) = bbox
subregion = im[y:y+h, x:x+w]
cv.imshow("lena subregion", subregion)
cv.waitKey(-1)
cv.destroyWindow("lena subregion")
How to extract ID document region of interest from image with OpenCV?
You're almost there, you just need to obtain the x,y,w,h
bounding rectangle coordinates using cv2.boundingRect
then you can extract/save the ROI using Numpy slicing. Here's the result
import cv2
# Load image, grayscale, threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
# Get bounding box and extract ROI
x,y,w,h = cv2.boundingRect(thresh)
ROI = image[y:y+h, x:x+w]
cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.waitKey()
Related Topics
Get Discord User Id from Username
How to Count the Number of Messages
How to Limit a Number to Be Within a Specified Range (Python)
Python: Fastest Way to Compare Arrays Elementwise
How to Make a Tkinter Label Background Transparent
Macos: How to Downgrade Homebrew Python
How to Clear/Delete the Contents of a Tkinter Text Widget
Python: How to Check If Cell in CSV File Is Empty
Python Selenium - Element Is Not Currently Interactable and May Not Be Manipulated
How to Change Python Version in Anaconda Spyder
Replace Single Quote With Double Quote in a String Python
How to Divide Each Column of Pandas Dataframe by a Series
How to Get Slope from Timeseries Data in Pandas
How to Execute Two Commands in Terminal Using Python'S Subprocess Module
Python 3 Error - Typeerror: Input Expected At Most 1 Arguments, Got 3