Created
March 25, 2014 14:19
-
-
Save ciela/9762788 to your computer and use it in GitHub Desktop.
OpenCV2系(Java)でモザイク処理 ref: http://qiita.com/ciela/items/f29fb91e28360b514307
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ROIを指定してモザイク処理 | |
* @param image 元画像となる {@link Mat} | |
* @param rectangles モザイク対象のROIを指定した {@link MatOfRect} | |
* @param size モザイクのピクセル幅 | |
* @return モザイク処理後の {@link Mat} | |
*/ | |
public static Mat drawMosaic(Mat image, MatOfRect rectangles, int size) { | |
Mat dstImage = image.clone(); | |
for (Rect rect : rectangles.toArray()) { | |
Mat imageROI = new Mat(image, rect); | |
Mat dstImageROI = new Mat(dstImage, rect); | |
for (int y = 0; y < imageROI.height(); y += size) { | |
for (int x = 0; x < imageROI.width(); x += size) { | |
int yLimit = y + size; | |
if (yLimit >= imageROI.height()) { | |
yLimit = imageROI.height(); | |
} | |
int xLimit = x + size; | |
if (xLimit >= imageROI.width()) { | |
xLimit = imageROI.width(); | |
} | |
double b, g, r; | |
b = g = r = 0; | |
int winSize = 0; | |
for (int i = y; i < yLimit; i++) { | |
for (int j = x; j < xLimit; j++) { | |
double[] pixel = imageROI.get(j, i); | |
b += pixel[0]; | |
g += pixel[1]; | |
r += pixel[2]; | |
winSize++; | |
} | |
} | |
b /= winSize; | |
g /= winSize; | |
r /= winSize; | |
for (int i = y; i < yLimit; i++) { | |
for (int j = x; j < xLimit; j++) { | |
dstImageROI.put(j, i, new double[] { b, g, r }); | |
} | |
} | |
} | |
} | |
} | |
return dstImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment