Skip to content

Commit

Permalink
Fix: Shrink filter
Browse files Browse the repository at this point in the history
Fixing shrink filter
  • Loading branch information
DiegoCatalano committed Jul 27, 2017
1 parent d0128ab commit b02592d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 204 deletions.
146 changes: 44 additions & 102 deletions Catalano.Android.Image/src/Catalano/Imaging/Filters/Shrink.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,78 +32,59 @@
* @author Diego Catalano
*/
public class Shrink implements IApplyInPlace{

private boolean isBlackObject = false;

/**
* Initializes a new instance of the Shrink class.
*/
public Shrink() {}

/**
* Initializes a new instance of the Shrink class.
* Only works in binary images.
* @param isBlackObject Object is black color.
*/
public Shrink(boolean isBlackObject){
this.isBlackObject = isBlackObject;
}

@Override
public void applyInPlace(FastBitmap fastBitmap) {

if(fastBitmap.isGrayscale()){

int gray = 255;
if(isBlackObject) gray = 0;

int width = fastBitmap.getWidth();
int height = fastBitmap.getHeight();

int minHeight = 0;
int minHeight = height;
int maxHeight = 0;
int minWidth = 0;
int minWidth = width;
int maxWidth = 0;

boolean found = false;

//Minimum height
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getGray(i*width+j) != 0){
minHeight = i;
found = true;
break;
if(fastBitmap.getGray(index++) == gray){
if(i < minHeight)
minHeight = i;
if(i > maxHeight)
maxHeight = i;
if(j < minWidth)
minWidth = j;
if(j > maxWidth)
maxWidth = j;
}
}
if (found) break;
}

found = false;

//Maximum height
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getGray(i*width+j) != 0){
maxHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Minimum width
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getGray(i*width+j) != 0){
minWidth = j;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum width
for (int j = width - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getGray(i*width+j) != 0){
maxWidth = j;
found = true;
break;
}
}
if (found) break;
// check
if ( ( minHeight == height ) && ( minWidth == width ) && ( maxHeight == 0 ) && ( maxWidth == 0 ) )
{
minHeight = minWidth = 0;
}

Crop crop = new Crop(minHeight, minWidth, maxWidth-minWidth+1, maxHeight-minHeight+1);
Expand All @@ -114,65 +95,26 @@ else if(fastBitmap.isRGB()){
int width = fastBitmap.getWidth();
int height = fastBitmap.getHeight();

int minHeight = 0;
int minHeight = height;
int maxHeight = 0;
int minWidth = 0;
int minWidth = width;
int maxWidth = 0;

boolean found = false;

//Minimum height
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
minHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum height
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
maxHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Minimum width
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
minWidth = j;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum width
for (int j = width - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
maxWidth = j;
found = true;
break;
if(fastBitmap.getRed(index) != 0 && fastBitmap.getGreen(index) != 0 && fastBitmap.getBlue(index) != 0){
if(i < minHeight)
minHeight = i;
if(i > maxHeight)
maxHeight = i;
if(j < minWidth)
minWidth = j;
if(j > maxWidth)
maxWidth = j;
}
index++;
}
if (found) break;
}

Crop crop = new Crop(minHeight, minWidth, maxWidth-minWidth+1, maxHeight-minHeight+1);
Expand Down
146 changes: 44 additions & 102 deletions Catalano.Image/src/Catalano/Imaging/Filters/Shrink.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,78 +32,59 @@
* @author Diego Catalano
*/
public class Shrink implements IApplyInPlace{

private boolean isBlackObject = false;

/**
* Initializes a new instance of the Shrink class.
*/
public Shrink() {}

/**
* Initializes a new instance of the Shrink class.
* Only works in binary images.
* @param isBlackObject Object is black color.
*/
public Shrink(boolean isBlackObject){
this.isBlackObject = isBlackObject;
}

@Override
public void applyInPlace(FastBitmap fastBitmap) {

if(fastBitmap.isGrayscale()){

int gray = 255;
if(isBlackObject) gray = 0;

int width = fastBitmap.getWidth();
int height = fastBitmap.getHeight();

int minHeight = 0;
int minHeight = height;
int maxHeight = 0;
int minWidth = 0;
int minWidth = width;
int maxWidth = 0;

boolean found = false;

//Minimum height
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getGray(i*width+j) != 0){
minHeight = i;
found = true;
break;
if(fastBitmap.getGray(index++) == gray){
if(i < minHeight)
minHeight = i;
if(i > maxHeight)
maxHeight = i;
if(j < minWidth)
minWidth = j;
if(j > maxWidth)
maxWidth = j;
}
}
if (found) break;
}

found = false;

//Maximum height
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getGray(i*width+j) != 0){
maxHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Minimum width
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getGray(i*width+j) != 0){
minWidth = j;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum width
for (int j = width - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getGray(i*width+j) != 0){
maxWidth = j;
found = true;
break;
}
}
if (found) break;
// check
if ( ( minHeight == height ) && ( minWidth == width ) && ( maxHeight == 0 ) && ( maxWidth == 0 ) )
{
minHeight = minWidth = 0;
}

Crop crop = new Crop(minHeight, minWidth, maxWidth-minWidth+1, maxHeight-minHeight+1);
Expand All @@ -114,65 +95,26 @@ else if(fastBitmap.isRGB()){
int width = fastBitmap.getWidth();
int height = fastBitmap.getHeight();

int minHeight = 0;
int minHeight = height;
int maxHeight = 0;
int minWidth = 0;
int minWidth = width;
int maxWidth = 0;

boolean found = false;

//Minimum height
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
minHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum height
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
maxHeight = i;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Minimum width
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
minWidth = j;
found = true;
break;
}
}
if (found) break;
}

found = false;

//Maximum width
for (int j = width - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
if(fastBitmap.getRed(i*width+j) != 0 && fastBitmap.getGreen(i*width+j) != 0 && fastBitmap.getBlue(i*width+j) != 0){
maxWidth = j;
found = true;
break;
if(fastBitmap.getRed(index) != 0 && fastBitmap.getGreen(index) != 0 && fastBitmap.getBlue(index) != 0){
if(i < minHeight)
minHeight = i;
if(i > maxHeight)
maxHeight = i;
if(j < minWidth)
minWidth = j;
if(j > maxWidth)
maxWidth = j;
}
index++;
}
if (found) break;
}

Crop crop = new Crop(minHeight, minWidth, maxWidth-minWidth+1, maxHeight-minHeight+1);
Expand Down
2 changes: 2 additions & 0 deletions Next Release notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Catalano Framework 1.6.2 road map
* Catalano.Imaging

- New: Ultimate Eroded Points.
- Added: DPI support when save the images in FastBitmap.
- Reworked: Fast Variance, now is really fast.
- Fixed: Shrink was slow and some images doesn't work.

* Catalano.MachineLearning

Expand Down

0 comments on commit b02592d

Please sign in to comment.