Skip to content

Instantly share code, notes, and snippets.

@ntrrgc
Created April 21, 2016 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntrrgc/f1381f0cc8d7ae88cc7381bfe840aedb to your computer and use it in GitHub Desktop.
Save ntrrgc/f1381f0cc8d7ae88cc7381bfe840aedb to your computer and use it in GitHub Desktop.
This program scans images in order to find white borders having no substantial content inside.
/*
* This program scans images in order to find white borders having no
* substantial content inside. This can be used to automatically crop
* meaningless parts of images.
*
* This program is designed to work only with white backgrounds.
*
* A Qt based test utility is provided in the main() function.
*
* Copyright (c) 2016 Juan Luis Boya
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdint.h>
#include <cmath>
/** A line will be considered as having content if 0.25% of it is filled. */
const float filledRatioLimit = 0.0025;
/** Colors with a value less than 127 will be considered black. */
const uint8_t filledValue = 127;
/** Returns the amount of color in a pixel. */
// Hopefully the optimizer will inline this function
int inline getPixelColorValue(const uint8_t* base, int width, int x, int y) {
// Aproximation: in order to avoid computing grayscale images, which would
// require averaging RGB, we just use one channel.
const uint8_t red = *(base + (y * width + x) * 3);
return red;
}
/** Return the first x position where there is a substancial amount of fill,
* starting the search from the left. */
int findBorderLeft(const uint8_t* img, int width, int height) {
int x, y;
const int filledLimit = round(height * filledRatioLimit);
// Scan vertical lines in search of filled lines
for (x = 0; x < width; x++) {
int filledCount = 0;
for (y = 0; y < height; y++) {
const uint8_t colorValue = getPixelColorValue(img, width, x, y);
bool filled = (colorValue < filledValue);
if (filled) {
filledCount++;
}
}
if (filledCount > filledLimit) {
// This line contains enough fill
return x;
}
}
// No fill found... don't crop anything
return 0;
}
/** Return the first x position where there is a substancial amount of fill,
* starting the search from the right. */
int findBorderRight(const uint8_t* img, int width, int height) {
int x, y;
const int filledLimit = round(height * filledRatioLimit);
// Scan vertical lines in search of filled lines
for (x = width - 1; x >= 0; x--) {
int filledCount = 0;
for (y = 0; y < height; y++) {
const uint8_t colorValue = getPixelColorValue(img, width, x, y);
bool filled = (colorValue < filledValue);
if (filled) {
filledCount++;
}
}
if (filledCount > filledLimit) {
// This line contains enough fill
return x;
}
}
// No fill found... don't crop anything
return width - 1;
}
/** Return the first y position where there is a substancial amount of fill,
* starting the search from the top. */
int findBorderTop(const uint8_t* img, int width, int height) {
int x, y;
const int filledLimit = round(width * filledRatioLimit);
// Scan horizontal lines in search of filled lines
for (y = 0; y < height; y++) {
int filledCount = 0;
for (x = 0; x < width; x++) {
const uint8_t colorValue = getPixelColorValue(img, width, x, y);
bool filled = (colorValue < filledValue);
if (filled) {
filledCount++;
}
}
if (filledCount > filledLimit) {
// This line contains enough fill
return y;
}
}
// No fill found... don't crop anything
return 0;
}
/** Return the first y position where there is a substancial amount of fill,
* starting the search from the bottom. */
int findBorderBottom(const uint8_t* img, int width, int height) {
int x, y;
const int filledLimit = round(width * filledRatioLimit);
// Scan horizontal lines in search of filled lines
for (y = height - 1; y >= 0; y--) {
int filledCount = 0;
for (x = 0; x < width; x++) {
const uint8_t colorValue = getPixelColorValue(img, width, x, y);
bool filled = (colorValue < filledValue);
if (filled) {
filledCount++;
}
}
if (filledCount > filledLimit) {
// This line contains enough fill
return y;
}
}
// No fill found... don't crop anything
return height - 1;
}
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QLabel>
#include <qdebug.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
view.show();
QImage image(argc > 1 ? argv[1] : "/tmp/test1.png");
QImage rgb = image.convertToFormat(QImage::Format_RGB888);
int top = findBorderTop(rgb.bits(), rgb.width(), rgb.height());
int bottom = findBorderBottom(rgb.bits(), rgb.width(), rgb.height());
int left = findBorderLeft(rgb.bits(), rgb.width(), rgb.height());
int right = findBorderRight(rgb.bits(), rgb.width(), rgb.height());
scene.addPixmap(QPixmap::fromImage(image));
QPen pen(QColor(50, 50, 255));
scene.addLine(0, top, image.width(), top, pen);
scene.addLine(0, bottom, image.width(), bottom, pen);
scene.addLine(left, 0, left, image.height(), pen);
scene.addLine(right, 0, right, image.height(), pen);
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment