Differentiate dark and light areas in image [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My problem is that I want to differentiate the light and dark areas in the following image to generate a binary mask.
https://i.stack.imgur.com/7ZRKB.jpg
An approximation to the output can be this:
https://i.stack.imgur.com/2UuJb.jpg
I've tried a lot of things but the results still have some noise or I lost a lot of data, like in this image:
https://i.stack.imgur.com/hUyjY.png
I've used python with opencv and numpy, gaussian filters, opening, closing, etc...
Somebody have some idea to doing this?
Thanks in advance!

I first reduced the size of the image using pyrDown then used CLAHE to equalize the histogram. I used medianblur as this will create patches then used opening 3 times. After that it was a simple binary_inv threshold. If you want to get the original image size, use cv2.pyrUp on image. By playing with the parameters you can manage to get better results.
import cv2
image = cv2.imread("7ZRKB.jpg",0)
image = cv2.pyrDown(image)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(16,16))
image = clahe.apply(image)
image = cv2.medianBlur(image, 7)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=3)
ret,image = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("image",image)
cv2.waitKey()
cv2.destroyAllWindows()
Result:

Related

Clear a heart trace with python [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 days ago.
Improve this question
for my thesis I'm working on a classifier (with Tensorflow) able to classifier if a heart trace contains or not an aritmia.
But I have a problem with the dataset: Practically, this is one image of my dataset:
The problem is that if we zoom on the trace we can see this:
Practically the outline of the curve has some kind of gradient around it. Could someone tell me how to eliminate this nuance in Python and maybe, how to increase the thickness of the stroke in order to highlight it?
Thanks a lot to everybody
Update 1:
I'm trying with this code, that seem resolve the problem, but when I apply cv2.dilate the image appear complete white.
import numpy as np
import cv2
for file in os.listdir("data/clean_test/original"):
img = image.load_img("data/clean_test/original/" + file, color_mode="grayscale")
img = image.img_to_array(img, dtype="uint8")
# do OTSU threshold to get circuit image
thresh = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 100
)
kernel = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(thresh, kernel, iterations=1)
print("Processed image: " + file)
cv2.imwrite(
"data/clean_test/new/" + os.path.splitext(file)[0] + ".png",
thresh,
[cv2.IMWRITE_PNG_COMPRESSION, 0],
)

Create a rectangle around a binary image mask [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
From a given image i'm able to create a binary mask that detect certain objects, how i can draw multiple rectangles a round those detected objects so that i're draw it to the original image also if it possible to obtain the corrdinates of those rectangle so i can plot them in the original image
As you haven't provide code, I will answer without code as well.
You should use findCountours. There is an opencv tutorial that helps you in this exact task: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html
cv2.findContours returns an array of contours, for each contour in contour you will need to:
x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

How to Contour Human Body in an image using PpenCv? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently trying to contour a human body from an image, but I am stuck right now.
I have taken different video lectures on contour, but they were related to objects like rectangles, circles and other simle shapes.
Can someone guide me in Human body contour? This picture shows an example of contour I am looking for.
You have to understand that detecting a human body is not so simple because it is hard to diferentiate the background from the body. That being said, if you have a simple background like the uploaded image, you can try to apply number of image tranformations (like applying binary threshold, otsu... look at opencv documentation - OpenCV documentation) to make your ROI "stand out" so you can detect with cv2.findContours() - same as drawing contour for circles, squares, etc. You can even apply cv2.Canny() (Canny edge detection) which detects a wide range of edges in the image and then search for contour. Here is an example for your image (the results could be better if the image didn't already have a red contour surrounding the body). Steps are desribed in comments in the code. Note that this is very basic stuff and would not work in most cases as the human detection is very difficult and broad question.
Example:
import cv2
# Read image and convert it to grayscale.
img = cv2.imread('human.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Search for edges in the image with cv2.Canny().
edges = cv2.Canny(img,150,200)
# Search for contours in the edged image with cv2.findContour().
_, contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Filter out contours that are not in your interest by applying size criterion.
for cnt in contours:
size = cv2.contourArea(cnt)
if size > 100:
cv2.drawContours(img, [cnt], -1, (255,0,0), 3)
# Display the image.
cv2.imshow('img', img)
Result:
Here is another useful link in the OpenCV documentation regarding this subject: Background Subtraction. Hope it helps a bit and gives you an idea on how to proceede. Cheers!

Compare frame of video with another image python? [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 1 year ago.
Improve this question
I would like compare a frame of video with another image but i don't know how can i do it with python.
Someone can help me please
You can use various metrics, look them up to see how they're calculated and when you should use them. In Python this can be achieved easily with scikit-image.
import cv2
from skimage.measure import compare_mse, compare_nrmse, compare_ssim, compare_psnr
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# mean squared error
compare_mse(img1, img2)
# normalized root-mean-square
compare_nrmse(img1, img2)
# peak signal-to-noise ratio
compare_psnr(img1, img2)
# structural similarity index
compare_ssim(img1, img2, multichannel=True)
The images must have the same size.

How to convert RGB to grayscale by using color dimensions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to transform my RGB image to grayscale image by not using converting function but with the red green blue values . For example, if my image is totally blue, it will be converted to white if I get blue components of it and it will be black if I get red components of my RGB image. It will be done in Python via OpenCV.
Thanks in advance.
The converting function that you are referring to does the same - it weights the R,G and B channel values of each pixel, and takes the sum. Since OpenCV uses the BGR colorspace on reading images, your conversion function will be something like this-
def rgbToGray(img):
grayImg = 0.0722*img(:,:,1) + 0.7152*img(:,:,2) + 0.2126*img(:,:,3)
return grayImg
The specific weights mentioned here are taken from the ITU-R BT.709 standard used for HDTV, developed by the ATSC (https://en.wikipedia.org/wiki/Grayscale)

Categories

Resources