Skip to content

Instantly share code, notes, and snippets.

@vonqo
Last active June 19, 2017 10:47
Show Gist options
  • Select an option

  • Save vonqo/bccc763fe9d6eba72bd89523ebcebf77 to your computer and use it in GitHub Desktop.

Select an option

Save vonqo/bccc763fe9d6eba72bd89523ebcebf77 to your computer and use it in GitHub Desktop.
Portrait Art #1 - Maaraa
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package portraitart;
import java.util.concurrent.ThreadLocalRandom;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import portraitart.util.ImgWindow;
/**
*
* @author lupino
*/
public class PortraitArt {
/**
* @param args the command line arguments
*/
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
String sourceImg = "alan_turing_1.jpg";
String tempImg = "edge.png";
Mat img = Highgui.imread(sourceImg);
Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(img, gray, new Size(0, 0), 5);
Core.addWeighted(img, 1.5, gray, -0.5, 0, gray);
Mat edges = new Mat();
int lowThreshold = 50;
int ratio = 3;
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);
Highgui.imwrite(tempImg, edges);
Mat edge = Highgui.imread(tempImg);
Mat lines = new Mat();
Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 10, 10, 10);
int dist = 3;
for (int i = 0; i < lines.cols(); i++) {
double[] val = lines.get(0, i);
Core.line(edge, new Point(val[0], val[1]), new Point(val[2], val[3]),
new Scalar(randColor(0, 100), randColor(0, 100), randColor(0, 100)), dist * 2);
}
for (int i = 0; i < lines.cols(); i++) {
double[] val = lines.get(0, i);
Core.line(edge, new Point(val[0] - dist, val[1] - dist), new Point(val[2] - dist, val[3] - dist),
new Scalar(0, 0, 255), 1);
Core.line(edge, new Point(val[0] + dist, val[1] + dist), new Point(val[2] + dist, val[3] + dist),
new Scalar(255, 0, 0), 1);
Core.line(edge, new Point(val[0] - dist, val[1] + dist), new Point(val[2] - dist, val[3] + dist),
new Scalar(0, 255, 0), 1);
}
Highgui.imwrite(tempImg, edge);
edge = Highgui.imread(tempImg);
Core.addWeighted(img, 0.1, edge, 1, 0, img);
Highgui.imwrite(tempImg, img);
}
public static int randColor(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment