我得到了下面的图像。我想获得矩形的 (x, y) 和宽度和高度。
我在下面发布了到目前为止我已经尝试过的内容,但它并没有给出好的结果。所以如果有人知道,请告诉我一个很好的算法来解决这个问题。提前致谢!
public static List<Rectangle> targetLists(final BufferedImage image) {
final List<List<Integer>> getPixels = getPixels(image);
final List<Rectangle> rectangleList = new ArrayList<Rectangle>();
final List<Integer> setX = new ArrayList<Integer>(new HashSet<Integer>(
getPixels.get(0)));
final List<Integer> setY = new ArrayList<Integer>(new HashSet<Integer>(
getPixels.get(1)));
List<Integer> xPointsList;
List<Integer> yPointsList;
if (setX != null) {
xPointsList = getListOfExactXandYpoints(setX);
if (xPointsList != null) {
yPointsList = getListOfExactXandYpoints(setY);
if (xPointsList.size() >= yPointsList.size()) {
return getRectangleListInXmostPoint(xPointsList,
yPointsList, rectangleList);
} else {
return getRectangleListInYmostPoint(yPointsList,
xPointsList, rectangleList);
}
}
}
return null;
}
private static List<List<Integer>> getPixels(BufferedImage image) {
final List<List<Integer>> lists = new ArrayList<List<Integer>>();
final List<Integer> targetsListX = new ArrayList<Integer>();
final List<Integer> targetsListY = new ArrayList<Integer>();
for(int y=0; y<image.getHeight(); y++) {
for(int x=0; x<image.getWidth(); x++) {
if(Color.BLACK.getRGB() == image.getRGB(x, y)) {
targetsListX.add(x);
targetsListY.add(y);
}
}
}
lists.add(targetsListX);
lists.add(targetsListY);
return lists;
}
private static List<Rectangle> getRectangleListInYmostPoint(
final List<Integer> list1, final List<Integer> list2,
final List<Rectangle> rectangleList) {
int step = 0;
for (int i = 0; i < list1.size(); i += 2) {
rectangleList.add(new Rectangle(new Point(list2.get(step), list1
.get(i)), new Dimension(Math.abs(list2.get(step)
- list2.get(step + 1)) + 1, Math.abs(list1.get(i)
- list1.get(i + 1) + 1))));
if (!((step + 2) >= list2.size())) {
step += 2;
}
}
return rectangleList;
}
private static List<Rectangle> getRectangleListInXmostPoint(
final List<Integer> list1, final List<Integer> list2,
final List<Rectangle> rectangleList) {
int step = 0;
for (int i = 0; i < list1.size(); i += 2) {
rectangleList.add(new Rectangle(new Point(list1.get(i), list2
.get(step)), new Dimension(Math.abs(list1.get(i)
- list1.get(i + 1)) + 1, Math.abs(list2.get(step)
- list2.get(step + 1) + 1))));
if (!((step + 2) >= list2.size())) {
step += 2;
}
}
return rectangleList;
}
private static List<Integer> getListOfExactXandYpoints(
final List<Integer> set) {
final List<Integer> list = new ArrayList<Integer>(set);
Collections.sort(list);
final ListIterator<Integer> iterator = list.listIterator();
final List<Integer> pointsSet = new ArrayList<Integer>();
int prev = 0;
while (iterator.hasNext()) {
final int i = iterator.next();
if (pointsSet.size() != 0) {
if (Math.abs(i - prev) > 15) {
pointsSet.add(prev);
pointsSet.add(i);
prev = i;
} else {
prev = i;
}
} else {
pointsSet.add(i);
prev = i;
}
}
if (pointsSet.size() != 0) {
if (pointsSet.size() % 2 == 0) {
pointsSet.remove(pointsSet.size() - 1);
}
pointsSet.add(list.get(list.size() - 1));
}
return pointsSet;
}