How can I remove black dots around hand in image - python

using opencv for capturing image in python
i want to make this image :
code for this :
# Image Processing
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (51,51), 15)
th3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
ret, test_image = cv2.threshold(th3, 10, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
to somewhat like this:

If you'll consider Scikit-Image, instead of OpenCV, here's an approach. It has been edited from my original post, thanks to guiding commentary from #Yves Daoust. This more correct approach runs significantly faster
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage import filters
from skimage import morphology
threshold = filters.threshold_otsu(image)
bimage = np.where(image>threshold,255,0)
rsh_img = morphology.remove_small_holes(bimage,area_threshold=33)

I think you should try closing operation. It will do the work.
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (51,51), 15)
th3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
ret, test_image = cv2.threshold(th3, 10, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
# opening the image
opening = cv2.morphologyEx(binr, cv2.MORPH_OPEN,
kernel, iterations=1)
If the resultant image is not good enough then try to increase he kernal size

Related

is there an easy way to binarize this ecg sample image?

iam a beginner at python and opencv. Iam interested about image processing and iam about to binarize this image.
[This is the image]
(https://i.stack.imgur.com/nKNKt.png)
[The final processed image]
(https://i.stack.imgur.com/R6La9.png)
this is what i try and the step that i implemented. Iam reading about image segmenting and grid removing. The thing that i understand is:
grayscaling image
blur the image
binarize the image
morphological operation
Here's the code
import cv2
import numpy as np
#load an image
img = cv2.imread('src.png')
#grayscaling image
gray0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#blur image-1
blurred0 = cv2.GaussianBlur(gray0, (3,3), 0)
#thresholding an image-1
t, thresh4 = cv2.threshold(blurred0, 158, 255, cv2.THRESH_BINARY_INV)
#creating kernel
kernel = np.ones((3,3), np.uint8)
#morphological operations
opening1 = cv2.morphologyEx(thresh4, cv2.MORPH_CLOSE, kernel)
#repeat blur-2
close = cv2.GaussianBlur(opening1, (3,3), 0)
#repeat thresholding-2
t, threshz = cv2.threshold(close, 127, 255, cv2.THRESH_BINARY)
#repeat blur-3
layer0 = cv2.GaussianBlur(threshz, (5,5), 0)
#repeat thresholding-3
t, threshz = cv2.threshold(layer0, 90, 255, cv2.THRESH_BINARY)
#morph close
threshzx = cv2.morphologyEx(threshz, cv2.MORPH_CLOSE, kernel)
i felt there's simple way than repeating this code to obtain the binarized image that we want. Thank you for your help.

How to use openCV to change white to black?

I have a bean on a white background damper, the issue is that the damper is not perfectly white (as in 255,255,255). I have tried using cv2.threshold() method but I kept getting a deformed image with spots. What is the best way to achieve this?
My Image
My Code
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
img[thresh == 255] = 0
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
erosion = cv2.erode(img, kernel, iterations = 1)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow("image", erosion)
In the end, the beans should only be surrounded by black images!
It could be the solution:
import cv2
import numpy as np
img = cv2.imread("data/bob.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blured = cv2.blur(gray, (5, 5))
ret, thres = cv2.threshold(img_blured, 130, 255, cv2.THRESH_BINARY)
neg = cv2.bitwise_not(thres)
erosion = cv2.erode(neg, np.ones((6, 6), np.uint8), iterations=1)
cv2.imshow("erosion", erosion)
# ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
img[erosion == 0] = 0
cv2.imshow("image", img)
cv2.waitKey(0)
Here I am using cv2.threshold but for bigger range than you used, but before it I blured the image. Then I negate and erode it.
But this cuts off the bean itself a little, if this is critical, then you should use a completely different algorithm. For example, the cv2.Canny method to find the contours of a bean and somehow further process it.

error at gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY

When i try to apply this code, i get a problem at line six.
can anyone tell me where is the problem exaclty?
import cv2
import numpy as np
img = cv2.imread('20170616_173327.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_c, cv2.THRESH_BINARY, 9, 9)
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)
cv2.imwrite("result.png", cartoon)
cv2.waitkey(0)
cv2.destroyAllWindows()
I had a similar issue, writing
gray = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
instead of gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) solwed my problem.
The problem was with the Colours library, for some reason I had to redownload and call all functions again, then it worked.

How to remove monitor flickering noise from an image?

I'm trying to remove a noise from a photo of a monitor screen. Here's the source photo:
I've tried some different approaches, so the current version of my code is as follows:
clr_img = cv2.imread("D:\Noisy.jpg", 1)
gray_img = cv2.cvtColor(clr_img, cv2.COLOR_BGR2GRAY)
gray_img = cv2.fastNlMeansDenoising(gray_img, h=11)
binary_image = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 91, 12)
Here's the result:
Is it possible to get rid of this kind of the noise?
You need to apply a smoothing operation before adaptive thresholding. A simple blur should help to reduce the noise. Any of these should work: Simple average blur (cv2.blur), Gaussian blur (cv2.GaussianBlur), or Median blur (cv2.medianBlur). Here's the result using a (7,7) Gaussian blur:
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,51,9)
result = 255 - thresh
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey()

Text Extraction from Image with Single letter in it

I have an image not of much good quality with a single letter in it. I need to extract the value from this
I tried doing this with open CV. the code works on good quality image but need help to extract from this image
from PIL import Image
import pytesseract
import argparse
import os
import cv2
import numpy as np
img = cv2.imread(r"/home/ubuntu/xyz/xyz.jpg")
img = cv2.resize(img, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.GaussianBlur(img, (5, 5), 0)
img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\[1\]
# Save the filtered image
cv2.imwrite(r"/home/ubuntu/xyz/rr.jpg", img)
# Read text with tesseract for python
result = pytesseract.image_to_string(img, lang="eng")
result
why u need Gaussian Blur in this situation
img = cv2.GaussianBlur(img, (5, 5), 0)
with a big window (5,5)
I think you can make a white border outside instead of resizing the image,
and you may use erosion technical to remove the noise from image

Categories

Resources