更改特定像素周围24个像素的最佳方法是什么?

时间:2015-02-02 15:23:09

标签: java opencv

我正在处理一段改变图像某些像素颜色的代码。它通过以下方法实现:

        inputImage.put(25, 25, colour);

这将选择 x -coordinate 25上的像素, y -coordinate 25,然后将其更改为指定的颜色

我现在必须将此像素及其周围的24个像素更改为新颜色,这意味着有一个5x5空间可以重新着色。

我可以想到几种方法。一种是迭代图像的所有像素,并检查它们是否在23和27之间,对于 x y - 坐标,然后如果它们重新着色它们。另一种是单独指定需要更改的每个像素。但这两种想法似乎都有点松弛。任何人都可以推荐一种更优雅的方式吗?

3 个答案:

答案 0 :(得分:2)

不要循环。

相反,设置完整的5x5 roi(在x,y):

int x=15,y=7;
in.submat(y-3,y+2, x-3,x+2).setTo(colour);

答案 1 :(得分:1)

也许将您的循环条件更改为从您的特定xy开始并在您的边界结束(可能是x+5y+5

示例:

int start = 23;
int boundary = start+5;    

for(int x = start; x < boundary; x++) {
    for(int y = start; y < boundary; y++) {
        inputImage.put(x,y,colour);
    }
}

答案 2 :(得分:-1)

正确的oop解决方案是定义Point类并创建Iterator以走你想要的范围。

class Point {

    public static final Point UNIT = new Point(1, 1);
    public static final Point X = new Point(1, 0);
    public static final Point Y = new Point(0, 1);
    public static final Point ZERO = new Point(0, 0);
    final int x;
    final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point(Point from) {
        this(from.x, from.y);
    }

    public Point add(Point delta) {
        return new Point(x + delta.x, y + delta.y);
    }

    public Point sub(Point delta) {
        return add(delta.negate());
    }

    public Point negate() {
        return new Point(-x, -y);
    }

    public String toString() {
        return "[" + x + "," + y + "]";
    }
}

class Around implements Iterator<Point> {

    private final Point center;
    private final Point range;
    private Point last = null;
    private Point next = null;

    public Around(Point center, Point range) {
        this.center = center;
        this.range = range;
    }

    @Override
    public boolean hasNext() {
        if (next == null) {
            if (last != null) {
                // Are we still in vertical range.
                if (last.y <= center.y + range.y) {
                    // Next is last step right one.
                    next = last.add(Point.X);
                    // Have we hit limit?
                    if (next.x > center.x + range.x) {
                        next = next.add(new Point((-2 * range.x) - 1, 1));
                        if (next.y >= center.y + range.y + 1) {
                            // Fallen out of y range.
                            next = null;
                        }
                    }
                }
            } else {
                // First = center - range.
                last = center.add(range.negate());
                next = last;
            }
        }
        return next != null;
    }

    @Override
    public Point next() {
        Point n = last = next;
        next = null;
        return n;
    }

}

public void test() {
    Iterator<Point> around = new Around(new Point(0, 0), new Point(2, 2));
    while (around.hasNext()) {
        System.out.println(around.next());
    }
}