Thresholding - OpenCV with Python for Image and Video Analysis 6

  Рет қаралды 125,365

sentdex

sentdex

Күн бұрын

Welcome to another OpenCV tutorial. In this tutorial, we'll be covering thresholding for image and video analysis. The idea of thresholding is to further-simplify visual data for analysis. First, you may convert to gray-scale, but then you have to consider that grayscale still has at least 255 values. What thresholding can do, at the most basic level, is convert everything to white or black, based on a threshold value. Let's say we want the threshold to be 125 (out of 255), then everything that was 125 and under would be converted to 0, or black, and everything above 125 would be converted to 255, or white. If you convert to grayscale as you normally will, you will get white and black. If you do not convert to grayscale, you will get thresholded pictures, but there will be color.
While that sounds good enough, it often isn't. We will be covering multiple examples and different types of thresholding here to illustrate this.
Sample code and text-based tutorial: pythonprogramming.net/thresho...
pythonprogramming.net
/ sentdex
/ pythonprogramming.net
plus.google.com/+sentdex

Пікірлер: 93
@JonesNanaz
@JonesNanaz 7 жыл бұрын
Your videos deserve to be seen more. they are some of the best on the net, and way better than most paid courses I have seen people paying for. Keep up the good work, and thanks for sharing your amazing skills
@colingnohz2403
@colingnohz2403 8 жыл бұрын
Thanks! Merry Christmas!
@tymothylim6550
@tymothylim6550 3 жыл бұрын
Thank you very much for this video! It was helpful for me :)
@bharatvarsha7
@bharatvarsha7 7 жыл бұрын
i used the same image as yours but the picture after gaussian threshold is not so clear. Used the block size parameter 115 like yours and found out the best result is with block size =9 but the words are not clear as yours. Any other way to get crisp clear image? thanks
@curtisgreen6874
@curtisgreen6874 8 жыл бұрын
Hello, thanks for all your videos, very helpful. I had question about thresholding. I am trying to track an object in low light and use a standard color threshold. The method I am trying is to first apply the threshold to the captured image and and then convert to hsv. Then create color arrays using np.array, then a mask to track a particular color, very similar to your red hat video. I think where I have issues is the color scheme on the threshold image is not what I think it is. Do you have any advice on how to track a color after a threshold has been applied? Thanks.
@user-gm1gb5ev7s
@user-gm1gb5ev7s 4 жыл бұрын
Спасибо, отличные уроки!
@saeedmardani3900
@saeedmardani3900 6 жыл бұрын
Like always, perfect, thanks
@erfanfekri7156
@erfanfekri7156 3 жыл бұрын
Nice vid , please Explain more about the input arguments in next vids
@adarshdubey6123
@adarshdubey6123 6 жыл бұрын
what are the last two values exactly?
@MariaSanchezQuezada
@MariaSanchezQuezada 8 жыл бұрын
Hey! How can I choose the 2 last parameters in adaptive thresholding? Thanks.
@dilkushmalav3478
@dilkushmalav3478 5 жыл бұрын
1)Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. 2)Constant subtracted from the mean or weighted mean . Normally, it is positive but may be zero or negative as well.
@danelarkin5424
@danelarkin5424 7 жыл бұрын
Love your tutorials! After a bit of fiddling I found that if you use cv2.THRESH_TRUNC with THRESH_OTSU instead of THRESH_BINARY you get really good results.
@sentdex
@sentdex 7 жыл бұрын
Thanks for sharing!
@NFM1337
@NFM1337 7 жыл бұрын
Would you be so kind as to provide osme details? I'm trying to do the same but end up with a very dark image.
@stanleychen6710
@stanleychen6710 Жыл бұрын
@@NFM1337 you should try to turn up your thresholding value
@qzorn4440
@qzorn4440 8 жыл бұрын
good job. thanks.
@Gaatech
@Gaatech 7 жыл бұрын
thanks for the video is there away to detect fire or heat
@gursimransingh234
@gursimransingh234 6 жыл бұрын
I am using a landscape picture in low light and i met with some error OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file D:\Build\OpenCV\opencv-3.3.1\modules\imgproc\src\color.cpp, line 11048
@PranshuDixit0105
@PranshuDixit0105 7 жыл бұрын
Hi, I'm trying to build an application in which I'm extracting text from an image. I'm using Pytesseract for that. For some images the output text of pytesseract is decent enough but for some it gives really weird output. So I wanted to pre process my image before giving it to pytesseract. So how do I generalise the thresholding for an image in any kind of lightning conditions in the application?
@Woodsy2013
@Woodsy2013 8 жыл бұрын
I keep getting this problem when running code. Traceback (most recent call last): File "E:/python/threshold.py", line 6, in cv2.imshow('original',img) cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\highgui\src\window.cpp:289: error: (-215) size.width>0 && size.height>0 in function cv::imshow I took the code straight from the website and it still happened does anyone know what my problem is?
@markph0204
@markph0204 6 жыл бұрын
Otsu generates a bi-modal histogram best for separating fore and backgrounds.
@RC2V6
@RC2V6 4 жыл бұрын
I imported an black and white image from a folder, and after reading and applying a threshold filter, i want to read the new threshold image generated instead of the original one i opened. How do i do this? Every time i try to read the thresholded image it reads the original BW imported. If you could help me, would be just awesome. I need to count the white pixels from the generated thresholded image, but it keeps reading the original BW. Thanks!
@JOSHITHARBEC
@JOSHITHARBEC 7 жыл бұрын
where can i find a list of all the thresholding functions from the opencv library?
@shehanjayawardane5727
@shehanjayawardane5727 7 жыл бұрын
hay, my question is that what do you actually meant by "Pixel value" in cv2.threshold the second parameter, is it the combination of BGR or B, G or R separately?
@abhishekshankar1136
@abhishekshankar1136 4 жыл бұрын
by default the threshold function takes only grayscaled images so its intensity wrt to 1 channel
@vinitjain6370
@vinitjain6370 6 жыл бұрын
why do we use retval1,retval2..cant we just give like one term?
@MontaRelaxingMusic
@MontaRelaxingMusic 8 жыл бұрын
Hi sir, I'm very enjoy your video, but I have a little question, can I save the picture(jpg) I made in this video with Python code?
@vishwas3019
@vishwas3019 8 жыл бұрын
+monta yen use cv2.imwrite('name.jpg',threshold)
@anwarawad6695
@anwarawad6695 5 жыл бұрын
I have a question i used in my code this function gray= cv2.threshold( gray , 0,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) [1] I wannna ask why [1] at the end ? My code doesn’t work without it ? What does it mean ?
@apocalypticcubing1771
@apocalypticcubing1771 8 жыл бұрын
I found that thresholding the colour version with 7 as the threshold it is really readable and if you use cv2.THRESH_BINARY_INVit makes it even easier to read.
@DineshShaw
@DineshShaw 4 жыл бұрын
what is retval in the line---> retval, threshold = cv2.threshold(img_gray, 12, 255, cv2.THRESH_BINARY) ?
@albertoherrerahp
@albertoherrerahp 7 жыл бұрын
I have been looking for a deep learning tutorial, or a neuronal network with python or java but i didnt have succes in my search. I have watch all your vids, but i dont understand yet if what you did was a deep learning with a neuronal network that opencv gives to you, i dont have clear all the theory yet. Can you do a tutorial making a neuronal network, and training it with deep learning please. This topic is real interesting to me, but in my country is just new, and at college isnt too much the interest that they gives to it. Thanks for make this vids man, now i know something about machine learning and all that stuff, i hope you keep uploading more vids like all this that you did upload. Thanks again! (sorry for my bad english jeje)
@malekbaba7672
@malekbaba7672 5 жыл бұрын
Search for andrew ng ' courses . They are the best.
@yigitokar
@yigitokar 6 жыл бұрын
Thank you for the tutorial videos. I found that If you implement adaptive thresholding with different values like 11,13,111,115 and add them using cv2.add() you can get a more readable version. (divide values by 2 while adding)
@thegoddessgeek1190
@thegoddessgeek1190 5 жыл бұрын
You did very well on this .. it is easy to follow
@sentdex
@sentdex 5 жыл бұрын
Great to hear!
@IsraelLopezMascorro
@IsraelLopezMascorro 7 жыл бұрын
Do you have some light changes example?
@Borja01
@Borja01 7 жыл бұрын
hi!!! First of all,congratulations for your work i have 1 question: i've written this code on Jupyter %matplotlib inline import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('1.png') img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh1 = cv2.threshold(img_grey,125,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) plt.imshow(thresh1) the original image was passed to BW on photoshop. However i found that I had to convert it to grayscale (DON'T UNDERSTAND WHY). My other problem is that when plotting, the img appears in a kind of dark blue-red colors. I hope u can help me and sorry for my gramatical mistakes. Thx!!!
@raghunandank2167
@raghunandank2167 7 жыл бұрын
what is the significance of block size and the last parameter
@SuperbooYouTube
@SuperbooYouTube 6 жыл бұрын
Hey 😀 I'm slightly confused on some of the parameters of cv2.threshold... what do arguments 2 & 3 (thresh and maxval) do? I'm playing around with it and I can't work out what changing the values is actually doing. Love all your videos, and thanks in advance to anyone with an answer
@stanleychen6710
@stanleychen6710 Жыл бұрын
hey superboo! imagine this : i have a grayscale image, every pixel will be made of numbers ranging from 0 to 255 right? if thresh is set to lets say 200, then any pixel that has a value under 200 gets turned in 0, or black in the THRESH_BINARY case. and any pixel that has a value greater than 200 gets turned into 1 or white.
@stanleychen6710
@stanleychen6710 Жыл бұрын
sry im not really sure about maxval
@weiwang6950
@weiwang6950 6 жыл бұрын
How to do the threshold on an uint16 bit tiff?
@AmitSharma-po1zb
@AmitSharma-po1zb 5 жыл бұрын
Hi, i have written this threshold code but receiving an error thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5) cv2.imshow("Adaptive Mean Thresholding", thresh) cv2.waitKey(0) and received this error:- --------------------------------------------------------------------------- error Traceback (most recent call last) in () 20 # using adaptiveThreshold 21 ---> 22 thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1) 23 cv2.imshow("Adaptive Mean Thresholding", thresh) 24 cv2.waitKey(0) error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1524: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold' Pls help
@hajirasharieff9121
@hajirasharieff9121 7 жыл бұрын
Heyyy Sentdex.! Simply love your videos.! New to OpenCv.! Your work is Amazinggg..! I tried this code and it works well,but I tried integrating the code for grayscale then noise removal and then Adaptive thresholding for a video.! but i have like million errors that i have no idea about..if you could please help me with that..it would be of a great help.! Please...!Thank you! more power to you! INDIA :)
@harivenkatesh5193
@harivenkatesh5193 7 жыл бұрын
last video u used threshold binary int in this u used threshold bin whats the diff
@XxXAMVILIKEITXxX
@XxXAMVILIKEITXxX 7 жыл бұрын
Hello, I have a question. What exactly is the meaning of ret and retval ? In previous tutorial (i.e Tutorial 5) also you have used ret,mask. I removed the ret from the code, and it gave me an error.
@ravilkashyap7407
@ravilkashyap7407 6 жыл бұрын
It probably returns True or False
@Freedster
@Freedster 8 жыл бұрын
I posted another comment for help before, because you respond more on newer videos, so heres my new trouble. Now that I'm using idle, I finished my other game. It was like a pick-your-path adventure thing. This new one however will be more of a game, so my question is (and ive looked through your videos) is there a way to do something like picking a random number, either out of a list, but preferably a range? e.i. : dmg = pickRandomNumber(1-100)
@Freedster
@Freedster 8 жыл бұрын
+Piggles The Pig Ok actually i found import random dmg = random.randint(1, 100) print(dmg) but is there a better way of doing this?
@sentdex
@sentdex 8 жыл бұрын
+Piggles The Pig I don't respond more on newer videos. I get comments in sequential order by top-level-comments, it has nothing to do with newer videos. I do not respond to comments purposefully on the wrong videos though. If I didn't respond to a recent top-level comment, then I just didn't have a reply for some reason.
@Freedster
@Freedster 8 жыл бұрын
sentdex Hmm.. when I commented on your input video I got no response. When I put a similar comment on your most recent video, i did get a response. I'll post them on the right videos, assuming I can find them.
@ricebastard
@ricebastard 7 жыл бұрын
Piggles the Pig pass the pigs please. By jove, a double jowler!
@liibvitiello8692
@liibvitiello8692 5 жыл бұрын
But man you really don't explain anything, I mean every function you use uses at least 6 variables and you just put numbers and commands like we knew it, I mean if I knew what you are writing I wouldn't come here in the first place
@blazecoolman
@blazecoolman 5 жыл бұрын
I love sentdex videos, but I was thinking the same thing. While I don't expect a handheld walkthrough, it would have been nice if he explained what were the parameters in adaptive thresholding. However, I also think that these videos are meant to get you into the workflow of using OpenCV following which you will have to do your own research into the documentation to find out what exactly you need. To answer your question, adaptive threshold is a modification of the simple threshold function and what it does is it calculates a threshold value for smaller sub-regions of the image instead of applying a value globally. The parameters used are. 1st - the variable referring to the image (which is a numpy array) 2nd - the maximum value of the numpy array (255 for white) 3rd - a flag specifying what type of adaptive thresholding will be used the next three values are specifying the components of this flag. 4th - the type of threshold used (THRESH) 5th - the size of the Gaussian window, i.e sub-region size (GAUSSIAN) 6th - a constant that subtracted from the threshold value for each sub-region (C) Source: docs.opencv.org/3.4.0/d7/d4d/tutorial_py_thresholding.html
@ubaidmanzoorwani7491
@ubaidmanzoorwani7491 5 жыл бұрын
Use this Link as a reference docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
@liibvitiello8692
@liibvitiello8692 5 жыл бұрын
thank you guys
@16rongon
@16rongon 6 жыл бұрын
If a threshold is converting a pixel value to either 255 or 0, then where are the other colors coming from in the resulting image?
@achrafhamal7128
@achrafhamal7128 5 жыл бұрын
I was thinking the same thing :o
@abhishekgoel8251
@abhishekgoel8251 5 жыл бұрын
@@achrafhamal7128 for grayscale it's only one dimension of values ranging b/w 0-225. Here he didn't convert it to grayscale in the first place, so it is in BGR format(i.e. 3 dimensions ). so it can have multiple cases like [0,225,0 ] or [225,0,0] for one pixel. Each of them corresponds to a different color, Hence there are multiple colors.
@rverm1000
@rverm1000 4 жыл бұрын
Might be a good for us who are unfamiliar with open CV to show documentation.
@asfandyarsaeed6402
@asfandyarsaeed6402 7 жыл бұрын
why have u writen 115,1
@mohannadaldakhil9068
@mohannadaldakhil9068 7 жыл бұрын
It's int block_size=115, double param1=1 blockSize - Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. C - Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.
@katesmith8624
@katesmith8624 6 жыл бұрын
the area to which this threshold "adapts"
@kantorobo7718
@kantorobo7718 4 жыл бұрын
Hey guys I keep getting the following error: Traceback (most recent call last): File "/Users/Chevre/Library/Preferences/PyCharmCE2018.2/scratches/scratch_16.py", line 9, in retval, threshold = cv2.threshold(img, 12, 255, cv2.TRESH_BINARY) AttributeError: module 'cv2.cv2' has no attribute 'TRESH_BINARY' Process finished with exit code 1 I am using PyCharm, is there a package I am missing? (working on Mac)
@adityachitrigemath762
@adityachitrigemath762 4 жыл бұрын
THRESH_BINARY instead of TRESH_BINARY should do the trick.
@ahanadrall5661
@ahanadrall5661 4 жыл бұрын
Can someone tell me how do the values of pixel change when we apply THRESH_BINARY on the colored image(like in the first bookpage example). What is the math behind the thresholding of colour images??? Thanks in advance.
@febinthomas1133
@febinthomas1133 3 жыл бұрын
I don't really know a lot about image operations but this is what I think: Each pixel have 3 channels - blue, green and red and 3 numbers to represent these 3 values. When you apply binary threshold to colored image, it will convert each channel in each pixel according to the parameter. Here, each 3 values in a pixel will be converted to 255 if it is more than 12.
@ahanadrall5661
@ahanadrall5661 3 жыл бұрын
@@febinthomas1133 ohhh yes this makes sense! Thanks a lot.
@prathamva7392
@prathamva7392 7 жыл бұрын
Heyy why did you copy gauss twice : ) ... 6:36
@achrafhamal7128
@achrafhamal7128 5 жыл бұрын
I was thinking the same thing :o
@manojkumarlinux
@manojkumarlinux 7 жыл бұрын
Video (moving object) Number Plate Detection python and open CV
@ZakReads
@ZakReads 8 жыл бұрын
You can do similar stuff in the pygame library too
@sentdex
@sentdex 8 жыл бұрын
+ItsTwigs We'll be leaving PyGame in the dust soon enough.
@ZakReads
@ZakReads 8 жыл бұрын
+sentdex sure did :D thanks for all the videos!
@lordnaive
@lordnaive 6 жыл бұрын
Hey! How to resize the windows..My pics are a bit big!!!
@MohammedHarisSumair
@MohammedHarisSumair 6 жыл бұрын
cv2.resizeWindow("windowname",height,width) :)
@lordnaive
@lordnaive 6 жыл бұрын
Thanks
@santiagojimenez4674
@santiagojimenez4674 6 жыл бұрын
cv2.resize(img, None,fx=number, fy=number, interpolation = cv2.INTER_CUBIC) resize a image
@hanchau414
@hanchau414 5 жыл бұрын
i leave this comment here, If anyone see this comment in 2021. please tag me to see what i have changed in 2 years since i learned sentdex.
@aliabbas632
@aliabbas632 9 ай бұрын
Hello give update...😊
@noonansean1979
@noonansean1979 7 жыл бұрын
Just FYI Gauss is pronounced with the vowel sound like the word "out", or "ouch". So it sounds like house but with a G in front not like gauze or gossip. And with Gaussian we don't pronounce the double s with an 'sh" sound. I know it's pedantic, and I know you're self taught but the mathematician Gauss is such an important figure it's kind of like hearing some say Einstone and then spelling his name Inestone. If you're going to name a variable after Gauss you may as well include both s letters too. It's just good form. Edit: Also I'm not trying to be rude here but helpful. It's hard being self taught, I know. But there are just certain names that when mathematicians or computer scientists hear them mispronounced they just cringe. The easiest to mispronounce being Euler.
@sentdex
@sentdex 7 жыл бұрын
You don't like my goshions?! Yeah, if I learn it from reading it, I often mispronounce terms. I don't mind the corrections, thanks.
@noonansean1979
@noonansean1979 7 жыл бұрын
Ah good, a sense of humor. Interestingly words like Hessians we do pronounce with the "sh". Who knows, strange rules? But I'd really suggest learning about Gauss. He was a really interesting person who contributed so much.
@saswatipalm.2300
@saswatipalm.2300 7 жыл бұрын
Sir, Thank You for such useful tutorials. I am new to python programming using opencv. I have a doubt in how to read the image files from a folder one-by-one. Please find some time from your busy schedule and help me.
@seyidcemkarakas1229
@seyidcemkarakas1229 4 жыл бұрын
ı realized in this video that he looks like jesse pinkman's nerd version
@marjohnsaycon235
@marjohnsaycon235 7 жыл бұрын
Am I the only one who noticed this? Is that you Snowden? hahaha
@sentdex
@sentdex 7 жыл бұрын
+Mar John Saycon youre not alone. I get daily comments about my cousin.
@mohammedal-alaw6249
@mohammedal-alaw6249 6 жыл бұрын
the main problem is the results are not like you presented
@mohammedal-alaw6249
@mohammedal-alaw6249 6 жыл бұрын
the first two images turned out to be the same
@lucavoros8073
@lucavoros8073 3 жыл бұрын
You did a very terrible job describing the threshold. May you please explain it a little better?
Hot Ball ASMR #asmr #asmrsounds #satisfying #relaxing #satisfyingvideo
00:19
Oddly Satisfying
Рет қаралды 28 МЛН
MOM TURNED THE NOODLES PINK😱
00:31
JULI_PROETO
Рет қаралды 33 МЛН
когда достали одноклассники!
00:49
БРУНО
Рет қаралды 4,3 МЛН
It was SO HARD to cut his hair
0:58
Jaybarber
Рет қаралды 21 МЛН
Image Thresholding in OpenCV
3:56
LearnOpenCV
Рет қаралды 700
HSV Color Range Thresholding - OpenCV Object Detection in Games #6
22:48
Learn Code By Gaming
Рет қаралды 48 М.
OpenCV Python Tutorial For Beginners 15 - Adaptive Thresholding
10:03
ProgrammingKnowledge
Рет қаралды 63 М.
Find and Draw Contours - OpenCV 3.4 with python 3 Tutorial 19
13:54
Hot Ball ASMR #asmr #asmrsounds #satisfying #relaxing #satisfyingvideo
00:19
Oddly Satisfying
Рет қаралды 28 МЛН