34 int w = image->width(), h = image->height();
36 QImage visited(w, h, QImage::Format_Mono);
42 int xmin = origin.x();
44 int ymin = origin.y();
47 QRgb opix = image->pixel(origin);
54 visited.setPixel(p, 1);
56 int x = p.x(), y = p.y();
58 if (x < xmin) xmin = x;
59 if (x > xmax) xmax = x;
61 if (y < ymin) ymin = y;
62 if (y > ymax) ymax = y;
64 std::stack<QPoint> neighbours;
66 int similarNeighbourCount = 0;
68 for (
int dx = -1; dx <= 1; ++dx) {
69 for (
int dy = -1; dy <= 1; ++dy) {
71 if ((dx != 0 && dy != 0) ||
75 if (x + dx < 0 || x + dx >= w ||
76 y + dy < 0 || y + dy >= h)
79 if (visited.pixelIndex(x + dx, y + dy) != 0)
82 if (!
similar(opix, image->pixel(x + dx, y + dy)))
85 neighbours.push(QPoint(x + dx, y + dy));
86 ++similarNeighbourCount;
90 if (similarNeighbourCount >= 2) {
91 while (!neighbours.empty()) {
92 s.push(neighbours.top());
98 return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
104 if (b == qRgb(0, 0, 0) || b == qRgb(255, 255, 255)) {
110 float ar = float(qRed(a)) / 255.f;
111 float ag = float(qGreen(a)) / 255.f;
112 float ab = float(qBlue(a)) / 255.f;
113 float amag = sqrtf(ar * ar + ag * ag + ab * ab);
114 float thresh = amag / 2;
116 float dr = float(qRed(a) - qRed(b)) / 255.f;
117 float dg = float(qGreen(a) - qGreen(b)) / 255.f;
118 float db = float(qBlue(a) - qBlue(b)) / 255.f;
119 float dist = sqrtf(dr * dr + dg * dg + db * db);
123 return (dist < thresh);
virtual ~ImageRegionFinder()
bool similar(QRgb a, QRgb b) const
QRect findRegionExtents(QImage *image, QPoint origin) const