java


OpenCV can't detect color red correctly [duplicate]


I have the following image:
I would like to detect the red rectangle using cv::inRange method and HSV color space.
int H_MIN = 0;
int H_MAX = 10;
int S_MIN = 70;
int S_MAX = 255;
int V_MIN = 50;
int V_MAX = 255;
cv::cvtColor( input, imageHSV, cv::COLOR_BGR2HSV );
cv::inRange( imageHSV, cv::Scalar( H_MIN, S_MIN, V_MIN ), cv::Scalar( H_MAX, S_MAX, V_MAX ), imgThreshold0 );
I already created dynamic trackbars in order to change the values for HSV, but I can't get the desired result.
Any suggestion for best values (and maybe filters) to use?
In HSV space, the red color wraps around 180. So you need the H values to be both in [0,10] and [170, 180].
Try this:
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
Mat3b bgr = imread("path_to_image");
Mat3b hsv;
cvtColor(bgr, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);
Mat1b mask = mask1 | mask2;
imshow("Mask", mask);
waitKey();
return 0;
}
Your previous result:
Result adding range [170, 180]:
Another interesting approach which needs to check a single range only is:
invert the BGR image
convert to HSV
look for cyan color
This idea has been proposed by fmw42 and kindly pointed out by Mark Setchell. Thank you very much for that.
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
Mat3b bgr = imread("path_to_image");
Mat3b bgr_inv = ~bgr;
Mat3b hsv_inv;
cvtColor(bgr_inv, hsv_inv, COLOR_BGR2HSV);
Mat1b mask;
inRange(hsv_inv, Scalar(90 - 10, 70, 50), Scalar(90 + 10, 255, 255), mask); // Cyan is 90
imshow("Mask", mask);
waitKey();
return 0;
}

Related Links

How to identify whether a web server supports Web Services or not?
How can I get the request URL from a Java Filter?
Caused by: java.sql.SQLException: Exhausted Resultset with Hibernate But NOT eclipselink
Javadoc: line breaks without HTML-tags?
Is EJB a ADO.NET equivalent in Java?
how could i make the JFrame content change to corresponding click?
What happens if multiple converters can unmarshal xml?
How do I read file x bytes at a time ? in java
Persistent instrumentation for Java
Error creating Uri object
Returning an inorder string of a Tree
In Maven, how do I customize lifecycle phases?
Windows XP blocking zip files created by my program
How to navigate in a circle
Why cannot I change a collection returned by getter?
Document Management System - Database Design

Categories

HOME
google-chrome-extension
mediawiki
nullpointerexception
iterator
q
programming-languages
filtering
spring-jdbc
amortized-analysis
ojdbc
datastax-java-driver
upload
add
amazon-cloudformation
nano-server
export-to-csv
windows-server-2012
systemc
google-static-maps
beyondcompare
scichart
reverse-proxy
scriptcs
intentfilter
chromium-embedded
crystal-reports-2010
tdd
fish
centos6.5
virtualdub
instant-messaging
bluestacks
data-manipulation
contact-form
winrt-xaml-toolkit
vsts-build-task
mozilla
mapbox-gl
x11-forwarding
squib
firebase-admin
password-encryption
http-redirect
sencha-touch-2.3
babel-core
apple-news
skeleton-css-boilerplate
dotcover
network-flow
theming
python-webbrowser
ionicons
blogengine.net
tizen-native-app
togetherjs
git-diff
brightcove
colorama
carrot
forever
tactic
libusb-win32
font-size
knpmenubundle
capacity
pearson
bind9
color-profile
blacklist
root-framework
android-fonts
spring-android
passport-google-oauth
radtreelist
database-optimization
underscore.js-templating
google-web-starter-kit
merge-conflict-resolution
plottable.js
xpath-1.0
cyclomatic-complexity
retina
fouc
ionic
graph-api-explorer
npapi
geonetwork
csquery
qpainter
fpml
wordpress-theme-customize
markers
flask-cors
xojo
typeof
elliptic-curve
mechanize-ruby
mcts
pyhdf
dataadapter
yui-compressor
orchardcms-1.7
mqx
factory-method
path-separator
unc
heartbeat
infobox
javax.script
yetanotherforum
netbeans-7.1
ecl
lang
google-friend-connect
castle-monorail
compiler-specific
substrings
adrotator
windows-live-messenger

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile