Skip to content

Commit

Permalink
sin city style filter
Browse files Browse the repository at this point in the history
  • Loading branch information
olir committed Apr 1, 2018
1 parent de8a33d commit faa1b84
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public boolean processVideo(ProcessingListener l) {
FilterContext context = new DefaultFilterContext();
for (FilterElement element : localFilters) {
if (element.filter != null) {
if (element.r.start <= i && i < element.r.end) {
if (element.r.start <= i && (i < element.r.end || !streamStopped)) {
// System.out.println("MovieProcessor
// processStreamFrame
// " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ public void createSegmentFilters() {
String pluginJogAmpRef = pluginJogAmpbase + "/" + "plugin-jogamp-" + version + ".jar";

// Filters completly in jar
// segmentFilters.add(loadExternalFilterInstance(
// "de.serviceflow.frankenstein.plugin.opencv.NativeExampleFilter", pluginOpenCVRef));
segmentFilters.add(loadExternalFilterInstance(
"de.serviceflow.frankenstein.plugin.opencv.NativeExampleFilter", pluginOpenCVRef));
segmentFilters.add(loadExternalFilterInstance(
"de.serviceflow.frankenstein.plugin.opencv.VideoEqualizerFilter", pluginOpenCVRef));
// segmentFilters.add(loadExternalFilterInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ public void videoStarted(int frames, double fps) {
currentFrameIndex.setDisable(false);
});

FilterElement val = new FilterElement(new Range(1, frames), this);
filterListData.add(val);
Platform.runLater(() -> {
drawEditCanvas();
});

}

private void adjustVideoLengthDisplay() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package de.serviceflow.frankenstein.plugin.opencv;

import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;
import javafx.fxml.FXML;
import javafx.scene.control.Slider;

public class NativeExampleConfigController extends SegmentConfigController {

@FXML
Slider slColor;
@FXML
Slider slRange;

private int farbe = 0;
private int range = 20;

public int getFarbe() {
return farbe;
}

void setFarbe(int farbe) {
this.farbe = farbe;
}

public void initialize() {
slColor.setValue(farbe);
slColor.valueProperty().addListener((observable, oldvalue, newvalue) -> {
farbe = newvalue.intValue();
fireChange();
});
slRange.setValue(range);
slRange.valueProperty().addListener((observable, oldvalue, newvalue) -> {
range = newvalue.intValue();
fireChange();
});
}

public void setRange(int range) {
this.range = range;
}

public int getRange() {
return range;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Method;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
Expand All @@ -30,11 +31,13 @@ public class NativeExampleFilter extends NativeSegmentFilter<NativeExampleConfig

private final Method jniProxyProcessMethod;

private Mat mHsvMat = new Mat();

@SuppressWarnings("unchecked")
public NativeExampleFilter() throws UnsatisfiedLinkError {
super("native", JNI_FILTER_CLASS);
try {
jniProxyProcessMethod = getJniProxyClass().getMethod("process", Object.class, int.class, Object.class);
jniProxyProcessMethod = getJniProxyClass().getMethod("process", Object.class, int.class, Object.class, int.class, int.class);
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException e) {
throw new RuntimeException("jni wrapper creation failed", e);
}
Expand All @@ -46,17 +49,29 @@ protected SegmentConfigController instantiateController() {
}

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
public Mat process(Mat rgbaImage, int frameId, FilterContext context) {
Imgproc.cvtColor(rgbaImage, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

NativeExampleConfigController c = ((NativeExampleConfigController)getConfigController());
int farbe = c.getFarbe();
int range = c.getRange();
try {
jniProxyProcessMethod.invoke(getJniProxy(), sourceFrame, frameId, context);
jniProxyProcessMethod.invoke(getJniProxy(), mHsvMat, frameId, context, farbe, range);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return sourceFrame;

Imgproc.cvtColor(mHsvMat, rgbaImage, Imgproc.COLOR_HSV2RGB_FULL);

return rgbaImage;
}

@Override
protected void initializeController() {
// getConfigController(). ...
NativeExampleConfigController c = ((NativeExampleConfigController)getConfigController());
int farbe = 310;
c.setFarbe(farbe);
int range = 20;
c.setRange(range);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ protected void loadLibrary(String name) {
}

public native void init();
public native void process(Object mat, int frameId, Object context);
public native void process(Object mat, int frameId, Object context, int farbe, int range);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,23 @@
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
</BorderPane>
<top>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="300.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="%lCOLOR" />
<Label text="%lRANGE" GridPane.rowIndex="1" />
<Slider fx:id="slColor" blockIncrement="60.0" majorTickUnit="60.0" max="359.0" minorTickCount="30" showTickLabels="true" showTickMarks="true" value="0.0" GridPane.columnIndex="1" />
<Slider fx:id="slRange" majorTickUnit="10.0" max="45.0" min="5.0" minorTickCount="1" showTickLabels="true" showTickMarks="true" value="20" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
</top>
</BorderPane>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
name=Native Example
name=Sin City Style
lCOLOR=Hue
lRANGE=Range
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name=Sin City Stil
lCOLOR=Farbwinkel
lRANGE=Breite
55 changes: 43 additions & 12 deletions plugin-opencv/native/src/main/native/NativeExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,66 @@ JNIEXPORT void JNICALL Java_de_serviceflow_frankenstein_plugin_opencv_jni_Native
(JNIEnv* env, jobject obj)
{
JwMat* mat = JwMat::matptr;
cout << "Java_cc0_NativeExample_init START" << endl;
if (mat == NULL) {
cout << "Java_cc0_NativeExample_init Mat-Wrapper created" << endl;
JwMat::matptr = new JwMat(env);
}
cout << "Java_cc0_NativeExample_init END" << endl;
}

JNIEXPORT void JNICALL Java_de_serviceflow_frankenstein_plugin_opencv_jni_NativeExample_process
(JNIEnv* env, jobject obj,
jobject matobj, jint frameId, jobject context)
jobject matobj, jint frameId, jobject context, jint keyHue, jint range)
{
cout << "Java_cc0_NativeExample_process CALLED " << frameId << endl;
JwMat* mat = JwMat::matptr;
int cols = mat->cols(env, matobj);
int rows = mat->rows(env, matobj);
cout << "rows=" << rows << ", cols=" << cols << endl;
// cout << "rows=" << rows << ", cols=" << cols << endl;

int channels = mat->channels(env, matobj);
if (channels<3) {
J_THROW("java/lang/Error", "channels < 3: "+mat->channels(env, matobj));
J_THROW("java/lang/Error", "Expecting HSV Mat. channels < 3: "+mat->channels(env, matobj));
return;
}

int c0 = (unsigned char)POINT_CHANNEL_VALUE(env,matobj,mat,(rows>>1),(cols>>1),0);
int c1 = (unsigned char)POINT_CHANNEL_VALUE(env,matobj,mat,(rows>>1),(cols>>1),1);
int c2 = (unsigned char)POINT_CHANNEL_VALUE(env,matobj,mat,(rows>>1),(cols>>1),2);
cout << "RGB-values at " << (rows>>1) << "," << (cols>>1) << ": " <<
c2 << "/" << c1 << "/" << c0 << endl ;
// keyHue := [0,359]

int saturation = 60;
int keyHueB = keyHue >> 1;

for(int y = 0; y < rows; y++)
{
jbyte * rowaddr = ROW_ADDR(env, matobj,mat,y);

for (int x = 0; x < cols; x++)
{
int i = x * channels;

// OpenCV: For HSV, Hue range is [0,179] mapped to [0,359], Saturation range is [0,255] and Value range is [0,255].
int h = 2 * (unsigned char)rowaddr[i];
int s = (unsigned char)rowaddr[i+1];
int v = (unsigned char)rowaddr[i+2];

int hlower = keyHue - range;
int hupper = keyHue + range;

if ( (h > hlower && h < hupper) || (hlower<0 && h>360+hlower) || (hupper>360 && h<hupper-360))
{
int distance = keyHue - h;
if (distance<0)
distance = -distance;

if (distance>=range-2)
s = s >>1;
if (distance>=range)
s = s >>1;
}
else
{
s = 0;
}

rowaddr[i+1] = s; // save result
}
}

}

0 comments on commit faa1b84

Please sign in to comment.