LaneDetection: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
https://github.com/naokishibuya/car-finding-lane-lines | https://github.com/naokishibuya/car-finding-lane-lines | ||
= Color Filter = | |||
[[File:show7141511869219163446.jpg|800px]] | [[File:show7141511869219163446.jpg|800px]] | ||
[[File:show6147902249785399207.jpg|800px]] | [[File:show6147902249785399207.jpg|800px]] | ||
== Source Code == | |||
=== ColorFilter === | |||
<source lang='java'> | |||
package org.rcdukes.video; | |||
import org.opencv.core.Core; | |||
import org.opencv.core.Mat; | |||
import org.opencv.core.Scalar; | |||
/** | |||
* filter colors in a given range | |||
* | |||
* @author wf | |||
* | |||
*/ | |||
public class ColorFilter { | |||
private Scalar minColor; | |||
private Scalar maxColor; | |||
/** | |||
* set minimum color with relative rgb values | |||
* | |||
* @param r | |||
* - red from 0.0 to 1.0 | |||
* @param g | |||
* - green from 0.0 to 1.0 | |||
* @param b | |||
* - blue from 0.0 to 1.0 | |||
*/ | |||
public void setMinColorRGB(double r, double g, double b) { | |||
this.minColor = new Scalar(b * 255, g * 255, r * 255); | |||
} | |||
/** | |||
* set the minimum rgb color | |||
* @param r red from 0 to 255 | |||
* @param g green from 0 to 255 | |||
* @param b blue from 0 to 255 | |||
*/ | |||
public void setMinColorRGB(int r, int g, int b) { | |||
this.minColor=new Scalar(b,g,r); | |||
} | |||
/** | |||
* set maximum color with rgb values | |||
* | |||
* @param r | |||
* @param g | |||
* @param b | |||
*/ | |||
public void setMaxColorRGB(double r, double g, double b) { | |||
this.maxColor = new Scalar(b * 255, g * 255, r * 255); | |||
} | |||
/** | |||
* set the maximum rgb color | |||
* @param r red from 0 to 255 | |||
* @param g green from 0 to 255 | |||
* @param b blue from 0 to 255 | |||
*/ | |||
public void setMaxColorRGB(int r, int g, int b) { | |||
this.maxColor=new Scalar(b,g,r); | |||
} | |||
/** | |||
* filter by the min and max Colors (if set) | |||
* | |||
* @param image | |||
* - the image to filter | |||
* @return - an image with colors in the given range or the original if not | |||
* both minColor and maxColor have been set | |||
*/ | |||
public Mat filter(Mat image) { | |||
Mat imgColorFiltered = image; | |||
if (minColor != null && maxColor != null) { | |||
// https://docs.opencv.org/3.4/da/d97/tutorial_threshold_inRange.html | |||
// https://stackoverflow.com/questions/36693348/java-opencv-core-inrange-input-parameters | |||
Mat imgColorMask = new Mat(); | |||
Core.inRange(image, minColor, maxColor, imgColorMask); | |||
imgColorFiltered = new Mat(); | |||
Core.bitwise_and(image, image, imgColorFiltered, imgColorMask); | |||
} | |||
return imgColorFiltered; | |||
} | |||
} | |||
</source> | |||
=== JUnit Test === | |||
<source lang='java'> | |||
@Test | |||
public void testColorFilter() throws Exception { | |||
ColorFilter cf=new ColorFilter(); | |||
cf.setMinColorRGB( 65, 85, 85); | |||
cf.setMaxColorRGB(140, 140, 140); | |||
Mat frame = getTestImage(); | |||
Mat gray=new Mat(); | |||
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY); | |||
assertEquals(2458261,Core.countNonZero(gray)); | |||
Mat colorFiltered = cf.filter(frame); | |||
assertEquals(colorFiltered.width(), frame.width()); | |||
Mat cfGray=new Mat(); | |||
Imgproc.cvtColor(colorFiltered, cfGray, Imgproc.COLOR_BGR2GRAY); | |||
assertEquals(173768,Core.countNonZero(cfGray)); | |||
if (show) { | |||
ImageUtils.show(getTestImage()); | |||
ImageUtils.show(gray); | |||
ImageUtils.show(colorFiltered); | |||
} | |||
} | |||
</source> | |||
Latest revision as of 07:12, 14 March 2020
Click here to comment see Self Driving RC Car
see https://github.com/naokishibuya/car-finding-lane-lines
Color Filter
Source Code
ColorFilter
package org.rcdukes.video;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
/**
* filter colors in a given range
*
* @author wf
*
*/
public class ColorFilter {
private Scalar minColor;
private Scalar maxColor;
/**
* set minimum color with relative rgb values
*
* @param r
* - red from 0.0 to 1.0
* @param g
* - green from 0.0 to 1.0
* @param b
* - blue from 0.0 to 1.0
*/
public void setMinColorRGB(double r, double g, double b) {
this.minColor = new Scalar(b * 255, g * 255, r * 255);
}
/**
* set the minimum rgb color
* @param r red from 0 to 255
* @param g green from 0 to 255
* @param b blue from 0 to 255
*/
public void setMinColorRGB(int r, int g, int b) {
this.minColor=new Scalar(b,g,r);
}
/**
* set maximum color with rgb values
*
* @param r
* @param g
* @param b
*/
public void setMaxColorRGB(double r, double g, double b) {
this.maxColor = new Scalar(b * 255, g * 255, r * 255);
}
/**
* set the maximum rgb color
* @param r red from 0 to 255
* @param g green from 0 to 255
* @param b blue from 0 to 255
*/
public void setMaxColorRGB(int r, int g, int b) {
this.maxColor=new Scalar(b,g,r);
}
/**
* filter by the min and max Colors (if set)
*
* @param image
* - the image to filter
* @return - an image with colors in the given range or the original if not
* both minColor and maxColor have been set
*/
public Mat filter(Mat image) {
Mat imgColorFiltered = image;
if (minColor != null && maxColor != null) {
// https://docs.opencv.org/3.4/da/d97/tutorial_threshold_inRange.html
// https://stackoverflow.com/questions/36693348/java-opencv-core-inrange-input-parameters
Mat imgColorMask = new Mat();
Core.inRange(image, minColor, maxColor, imgColorMask);
imgColorFiltered = new Mat();
Core.bitwise_and(image, image, imgColorFiltered, imgColorMask);
}
return imgColorFiltered;
}
}
JUnit Test
@Test
public void testColorFilter() throws Exception {
ColorFilter cf=new ColorFilter();
cf.setMinColorRGB( 65, 85, 85);
cf.setMaxColorRGB(140, 140, 140);
Mat frame = getTestImage();
Mat gray=new Mat();
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
assertEquals(2458261,Core.countNonZero(gray));
Mat colorFiltered = cf.filter(frame);
assertEquals(colorFiltered.width(), frame.width());
Mat cfGray=new Mat();
Imgproc.cvtColor(colorFiltered, cfGray, Imgproc.COLOR_BGR2GRAY);
assertEquals(173768,Core.countNonZero(cfGray));
if (show) {
ImageUtils.show(getTestImage());
ImageUtils.show(gray);
ImageUtils.show(colorFiltered);
}
}