doni
May 9, 2023, 2:06pm
1
hi forum anyone could help me how to change following code
int lowerb = 100;
int upperb = 145;
into this
int lowerb = ([100,0,0]);
int upperb = ([255,80,255]);
below is complete code
import gab.opencv.*;
import processing.video.*;
PImage src, colorFilteredImage;
OpenCV opencv;
Movie video;
int lowerb = 100;//80,100
int upperb = 145;//148
Histogram histogram;
// movie used:
// https://github.com/processing/processing-video/raw/main/examples/Movie/Speed/data/launch2.mp4
int count;
void setup()
{
size(1120, 406);
video = new Movie(this, "launch2.mp4");
println(video.width, video.height);
video.loop();
while(video.width <=0)
{
delay(1);
}
println(video.width, video.height);
opencv = new OpenCV(this, video.width, video.height);
while(opencv.width<=0)
{
delay(1);
}
println(opencv.width, opencv.height);
}
void draw()
{
image(video, 0, 0);
opencv.loadImage(video);
opencv.useColor(HSB);
opencv.setGray(opencv.getH().clone());
opencv.inRange(lowerb, upperb);
histogram = opencv.findHistogram(opencv.getH(), 255);
image(opencv.getOutput(), video.width, 0);
}
void movieEvent(Movie m)
{
m.read();
}
thank you.
It looks like inRange() takes integers, not integer triples (or arrays). So you only want to pass it two single integer values.
doni
May 9, 2023, 3:02pm
4
i know but in the source it receive scalar
public void inRange(int lowerBound, int upperBound){
Core.inRange(getCurrentMat(), new Scalar(lowerBound), new Scalar(upperBound), getCurrentMat());
}
as far as i know scalar could be written as follow
Scalar lowerb = new Scalar (0,0,0)
Looking at https://docs.opencv.org/3.4/javadoc/org/opencv/core/Scalar.html , it looks like their Scalar class takes double
s in the constructor, so you could have
double[] lowerbnd = { 100, 0, 0 };
double[] upperbnd = { 255, 80, 255 };
Scalar lowerb;
Scalar upperb;
void setup() {
lowerb = new Scalar( lowerbnd );
upperb = new Scalar( upperbnd );
}
but recognize that you’ll have to call the Core.inRange()
version of the function yourself. The gab
wrapper only takes a single int
and not a Scalar
.
doni
May 9, 2023, 4:09pm
6
its error with
Type mismatch: cannot convert from Scalar to double
Okay please show your code.
It’s
btw.
doni
May 9, 2023, 5:02pm
8
import org.opencv.core.Core;
import gab.opencv.*;
import processing.video.*;
PImage src, colorFilteredImage;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
OpenCV opencv;
Movie video;
//int lowerb = 100;//80,100
//int upperb = 145;//
double[] lowerbnd = { 100, 0, 0 };
double[] upperbnd = { 255, 80, 255 };
Histogram histogram;
void setup() {
size(1136, 320);
video = new Movie(this, "vid1.mp4");//1
opencv = new OpenCV(this, 568, 320);
lowerbnd = new Scalar( lowerbnd );
upperbnd = new Scalar( upperbnd );
video.loop();
video.play();
}
void draw() {
if (video.width == 0 || video.height == 0)
return;
opencv.loadImage(video);
opencv.useColor(HSB);
image(video, 0, 0);
//opencv.useColor(HSB); able to display the result but strange image
opencv.setGray(opencv.getH().clone());
opencv.inRange(lowerbnd, upperbnd);
histogram = opencv.findHistogram(opencv.getH(), 255);
//opencv.useColor(HSB);// this not work
// translate(video.width, 0);
image(opencv.getOutput(), video.width, 0);
}
void movieEvent(Movie m)
{
m.read();
}
opencv.inRange() takes two ints. You can’t give it anything other than two ints.
Its code, however, calls Core.inRange() which takes different parameters. You need to call that function if you want to pass it Scalar objects. I’m not sure how you would go about doing so though.
just for completeness:
remember that scudly had
double[] lowerbnd = { 100, 0, 0 };
double[] upperbnd = { 255, 80, 255 };
Scalar lowerb;
Scalar upperb;
and
lowerb = new Scalar( lowerbnd );
upperb = new Scalar( upperbnd );
Where you have
lowerbnd = new Scalar( lowerbnd );
upperbnd = new Scalar( upperbnd );
can’t work this way.
You need lowerb and upperb.
doni
May 9, 2023, 5:36pm
11
actually i succeed in android studio just to filter white image with this code, and just want to try in processing
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private int w, h;
private CameraBridgeViewBase mOpenCvCameraView;
TextView tvName;
Scalar sc1 = new Scalar(80, 0, 0);
Scalar sc2 = new Scalar(250, 160, 250);
int iLowH = 80;
int iHighH = 255;
int iLowS = 0;
int iHighS = 160;
int iLowV = 0;
int iHighV = 255;
Mat mRgba, imgHSV, imgThreshold, fit;
Scalar sc1,sc2;
static {
if (!OpenCVLoader.initDebug())
Log.d("ERROR", "Unable to load OpenCV");
else
Log.d("SUCCESS", "OpenCV loaded");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
public void onCameraViewStopped() {
}
public void onCameraViewStarted(int width, int height) {
imgHSV = new Mat(width,height, CvType.CV_16UC4);
imgThreshold = new Mat(width,height, CvType.CV_16UC4);
}
public MainActivity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.display);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
sc1 = new Scalar(iLowH,iLowS,iLowV);
sc2 = new Scalar(iHighH,iHighS,iHighV);
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Imgproc.cvtColor(inputFrame.rgba(), imgHSV, Imgproc.COLOR_BGR2HSV);
Core.inRange(imgHSV,sc1,sc2,imgThreshold);
return imgThreshold;
}
}
doni
May 9, 2023, 5:41pm
12
i try this but still the opencv.inRange(); expects (int,int);
import org.opencv.core.Core;
import gab.opencv.*;
import processing.video.*;
PImage src, colorFilteredImage;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
OpenCV opencv;
Movie video;
//int lowerb = 100;//80,100
//int upperb = 145;//
Scalar lowerb;
Scalar upperb;
double[] lowerbnd = { 100, 0, 0 };
double[] upperbnd = { 255, 80, 255 };
Histogram histogram;
void setup() {
size(1136, 320);
video = new Movie(this, "vid1.mp4");//1
opencv = new OpenCV(this, 568, 320);
lowerb = new Scalar( lowerbnd );
upperb = new Scalar( upperbnd );
video.loop();
video.play();
}
void draw() {
if (video.width == 0 || video.height == 0)
return;
opencv.loadImage(video);
opencv.useColor(HSB);
image(video, 0, 0);
//opencv.useColor(HSB); able to display the result but strange image
opencv.setGray(opencv.getH().clone());
opencv.inRange(lowerb, upperb);
histogram = opencv.findHistogram(opencv.getH(), 255);
//opencv.useColor(HSB);// this not work
// translate(video.width, 0);
image(opencv.getOutput(), video.width, 0);
}
void movieEvent(Movie m)
{
m.read();
}
1 Like
I can’t help you with this
1 Like