I got the contours of a box with the cartoonEdgeFilter. Then I made an Image out of the Frambuffer.
Now I iterate over all pixels of the image and try to add an error to each pixel which simulates the shaking, with this function:
y = asin(bx+c)+r
a and b determine the range and period of the error
c is a shift
r is randomness
It should look like in this picture:
But I only get unsatisfying results, which look like this:
My Code:
raster = ImageRaster.create(image);
ImageRaster rasterDistorted = ImageRaster.create(imageDistorted);
ColorRGBA color;
Random random = new Random();
int randomVal = 0;
for (int x = 0; x < 640; x++){
for(int y = 0; y < 480;y++){
randomVal = (random.nextInt(6)-3);
int yOld = (int)(5 * Math.sin(0.2*y + 1)+ randomVal)+ y;
int xOld = (int)(5 * Math.sin(0.2*x + 1)+ randomVal)+ x;
if (xOld > 639) {
xOld = 639;
}
if (xOld < 0) {
xOld = 0;
}
if (yOld > 479) {
yOld = 479;
}
if (yOld < 0) {
yOld = 0;
}
color = raster.getPixel(xOld, yOld);
rasterDistorted.setPixel(x, y, color);
}
}
Where did you get the formula? Some context might be helpful.
In the first sample drawing, I can’t see how the formula would produce those nice results so evenly on both the mostly horizontal and mostly vertical edges. Ah, you use it for x and y… makes more sense.
In your second pic, it looks like things are just too random. What does it look like if you remove randomVal? Also note that with 5*sin you will vary by a full 10 pixels. And using 0.2 as the period multiplier means you go through a full sine cycle in just 30 pixels or so… which seems pretty dramatic to me.
As I implied, your variance is too high and too fast… so you will skip entire pixels because you’ve compressed them out of the image.
Try varying just the y and you’ll see what I mean… every teeny tiny bump is one that would have potentially been missed if x happened to bump right there also. You actually increased your (a) above which will make it even worse because now the pixels will vary by as much as 40 pixels overall… when something like a=4 (8 variance) is probably more than fine.
Bottom line: you need to start with teeny tiny numbers and slowly increase them to see what affect you get.
Edit: also I suspect the original paper might have been subsampling where as you are hitting discrete pixels. This is the kind of thing that would be better done in a shader, perhaps.
I’m not sure what this “exchange” means. I suggested that you only calculate the y offset. Leave xOld = x;
I can’t be sure what you’ve done with x in the picture so I can only guess.
The issue is that because your variances are high and you are sampling at even pixels then you will have gaps where you don’t pick up any pixels at all. If the black happens to be in those gaps then you will miss stuff.
…actually the bars on the left that are solid black definitely indicate something is wrong with some of your calculations.