piuspatch
v0.1
USB addition to PIE backend
|
#include <stdlib.h>
#include <string.h>
#include <values.h>
#include <math.h>
#include "../include/sane/sane.h"
#include "../include/sane/sanei_debug.h"
#include "../include/sane/sanei_ir.h"
#include "../include/sane/sanei_magic.h"
Go to the source code of this file.
Defines | |
#define | BACKEND_NAME sanei_ir /* name of this module for debugging */ |
Functions | |
double * | sanei_ir_create_norm_histo (const SANE_Parameters *params, const SANEI_IR_bufptr img_data) |
double * | sanei_ir_accumulate_norm_histo (double *histo_data) |
void | sanei_ir_init (void) |
SANE_Status | sanei_ir_create_norm_histogram (const SANE_Parameters *params, const SANEI_IR_bufptr img_data, double **histogram) |
Create the normalized histogram of a grayscale image. | |
SANE_Status | sanei_ir_threshold_yen (const SANE_Parameters *params, double *norm_histo, int *thresh) |
Implements Yen's thresholding method. | |
SANE_Status | sanei_ir_threshold_otsu (const SANE_Parameters *params, double *norm_histo, int *thresh) |
Implements Otsu's thresholding method. | |
SANE_Status | sanei_ir_threshold_maxentropy (const SANE_Parameters *params, double *norm_histo, int *thresh) |
Implements a Maximum Entropy thresholding method. | |
SANE_Status | sane_ir_RGB_luminance (SANE_Parameters *params, const SANEI_IR_bufptr *in_img, SANEI_IR_bufptr *out_img, SANE_Bool scale8) |
Generate gray scale luminance image from separate R, G, B images. | |
SANE_Status | sane_ir_to_8bit (SANE_Parameters *params, const SANEI_IR_bufptr in_img, SANE_Parameters *out_params, SANEI_IR_bufptr *out_img) |
Convert image from >8 bit depth to an 8 bit image. | |
SANE_Status | sane_ir_ln_table (int len, double **lut_ln) |
Allocate and initialize logarithmic lookup table. | |
SANE_Status | sane_ir_spectral_clean (const SANE_Parameters *params, double *lut_ln, const SANEI_IR_bufptr red_data, SANEI_IR_bufptr ir_data) |
Reduces red spectral overlap from an infrared image plane. | |
SANE_Status | sanei_ir_filter_mean (const SANE_Parameters *params, const SANEI_IR_bufptr in_img, SANEI_IR_bufptr out_img, int win_rows, int win_cols) |
Optimized mean filter. | |
SANE_Status | sanei_ir_filter_madmean (const SANE_Parameters *params, const SANEI_IR_bufptr in_img, SANE_Byte **out_img, int win_size, int a_val, int b_val) |
Find noise by adaptive thresholding. | |
void | sanei_ir_add_threshold (const SANE_Parameters *params, const SANEI_IR_bufptr in_img, SANE_Byte *mask_img, int threshold) |
Add dark pixels to mask from static threshold. | |
void | sane_ir_manhattan_dist (const SANE_Parameters *params, const SANE_Byte *mask_img, unsigned int *dist_map, unsigned int *idx_map, unsigned int erode) |
Calculates minimal Manhattan distances for an image mask. | |
void | sane_ir_dilate (const SANE_Parameters *params, SANE_Byte *mask_img, unsigned int *dist_map, unsigned int *idx_map, int by) |
Dilate or erode a mask image. | |
void | sanei_ir_find_crop (const SANE_Parameters *params, unsigned int *dist_map, int inner, int *edges) |
Suggest cropping for dark margins of positive film. | |
SANE_Status | sanei_ir_dilate_mean (const SANE_Parameters *params, SANEI_IR_bufptr *in_img, SANE_Byte *mask_img, int dist_max, int expand, int win_size, SANE_Bool smooth, int inner, int *crop) |
Dilate clean image parts into dirty ones and smooth int inner,. |
sanei_ir - functions for utilizing the infrared plane
Copyright (C) 2012 Michael Rickmann <mrickma@gwdg.de>
This file is part of the SANE package.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The threshold yen, otsu and max_entropy routines have been adapted from the FOURIER 0.8 library by M. Emre Celebi, http://sourceforge.net/projects/fourier-ipal/ which is licensed under the GNU General Public License version 2 or later.
Definition in file sanei_ir.c.
#define BACKEND_NAME sanei_ir /* name of this module for debugging */ |
Definition at line 35 of file sanei_ir.c.
void sane_ir_dilate | ( | const SANE_Parameters * | params, |
SANE_Byte * | mask_img, | ||
unsigned int * | dist_map, | ||
unsigned int * | idx_map, | ||
int | by | ||
) |
Dilate or erode a mask image.
[in] | params | pointer to image description |
mask_img | pointer to binary image (0, 255) | |
dist_map | integer pointer to map of closest distances | |
idx_map | integer pointer to indices of closest pixels | |
[in] | by | number of pixels, > 0 dilate, < 0 erode |
Definition at line 1169 of file sanei_ir.c.
SANE_Status sane_ir_ln_table | ( | int | len, |
double ** | lut_ln | ||
) |
Allocate and initialize logarithmic lookup table.
[in] | length | of table, usually 1 << depth |
[out] | lut_ln | adress of pointer to allocated table |
Definition at line 550 of file sanei_ir.c.
void sane_ir_manhattan_dist | ( | const SANE_Parameters * | params, |
const SANE_Byte * | mask_img, | ||
unsigned int * | dist_map, | ||
unsigned int * | idx_map, | ||
unsigned int | erode | ||
) |
Calculates minimal Manhattan distances for an image mask.
[in] | params | pointer to image description |
[in] | mask_img | pointer to binary image (0, 255) |
[out] | dist_map | integer pointer to map of closest distances |
[out] | idx_map | integer pointer to indices of closest pixels |
[in] | erode | == 0: closest pixel has value 0, != 0: is 255 |
manhattan_dist takes a mask image consisting of 0 or 255 values. Given that a 0 represents a dirty pixel and erode != 0, manhattan_dist will calculate the shortest distance to a clean (255) pixel and record which pixel that was so that the clean parts of the image can be dilated into the dirty ones. Thresholding can be done on the distance. Conversely, if erode == 0 the distance of a clean pixel to the closest dirty one is calculated which can be used to dilate the mask.
extended and C version of http://ostermiller.org/dilate_and_erode.html
Definition at line 1063 of file sanei_ir.c.
SANE_Status sane_ir_RGB_luminance | ( | SANE_Parameters * | params, |
const SANEI_IR_bufptr * | in_img, | ||
SANEI_IR_bufptr * | out_img, | ||
SANE_Bool | scale8 | ||
) |
Generate gray scale luminance image from separate R, G, B images.
params | points to image description | |
[in] | in_img | pointer to at least 3 planes of image data |
[out] | out_img | newly allocated image |
[in] | scale8 | if true output image will be 8 bit deep |
Definition at line 432 of file sanei_ir.c.
SANE_Status sane_ir_spectral_clean | ( | const SANE_Parameters * | params, |
double * | lut_ln, | ||
const SANEI_IR_bufptr | red_data, | ||
SANEI_IR_bufptr | ir_data | ||
) |
Reduces red spectral overlap from an infrared image plane.
[in] | params | pointer to image description |
[in] | lut_ln | pointer lookup table if NULL it is dynamically handled |
[in] | red_data | pointer to red image plane |
ired_data | pointer to ired image plane |
This routine is based on the observation that the relation beween the infrared value ired and the red value red of an image point can be described by ired = b + a * ln (red). First points are randomly sampled to calculate the linear regression coefficent a. Then ired' = ired - a * ln (red) is calculated for each pixel. Finally, the ir' image is scaled between 0 and maximal value. For the logarithms a lookup table is used. Negative films show very little spectral overlap but positive film usually has to be cleaned. As we do a statistical measure of the film here dark margins and lumps of dirt have to be excluded.
Definition at line 576 of file sanei_ir.c.
SANE_Status sane_ir_to_8bit | ( | SANE_Parameters * | params, |
const SANEI_IR_bufptr | in_img, | ||
SANE_Parameters * | out_params, | ||
SANEI_IR_bufptr * | out_img | ||
) |
Convert image from >8 bit depth to an 8 bit image.
[in] | params | pimage description |
[in] | in_img | points to input image data |
[out] | out_params | if != NULL receives description of new image |
[out] | out_img | newly allocated 8-bit image |
Definition at line 494 of file sanei_ir.c.
double * sanei_ir_accumulate_norm_histo | ( | double * | histo_data | ) |
Definition at line 151 of file sanei_ir.c.
void sanei_ir_add_threshold | ( | const SANE_Parameters * | params, |
const SANEI_IR_bufptr | in_img, | ||
SANE_Byte * | mask_img, | ||
int | threshold | ||
) |
Add dark pixels to mask from static threshold.
[in] | params | pointer to image description |
[in] | in_img | pointer to grey scale image |
mask_img | pointer to binary image (0, 255) | |
[in] | threshold | below which the pixel is set 0 |
Definition at line 1029 of file sanei_ir.c.
double * sanei_ir_create_norm_histo | ( | const SANE_Parameters * | params, |
const SANEI_IR_bufptr | img_data | ||
) |
Definition at line 61 of file sanei_ir.c.
SANE_Status sanei_ir_create_norm_histogram | ( | const SANE_Parameters * | params, |
const SANEI_IR_bufptr | img_data, | ||
double ** | histogram | ||
) |
Create the normalized histogram of a grayscale image.
[in] | params | describes image |
[in] | img_data | image pointer { grayscale } |
[out] | histogram | an array of double with histogram |
Definition at line 132 of file sanei_ir.c.
SANE_Status sanei_ir_dilate_mean | ( | const SANE_Parameters * | params, |
SANEI_IR_bufptr * | in_img, | ||
SANE_Byte * | mask_img, | ||
int | dist_max, | ||
int | expand, | ||
int | win_size, | ||
SANE_Bool | smooth, | ||
int | inner, | ||
int * | crop | ||
) |
Dilate clean image parts into dirty ones and smooth int inner,.
[in] | params | pointer to image description |
in_img | array of pointers to color planes of image | |
[in] | mask_img | pointer to dirt mask image |
[in] | dist_max | threshold up to which dilation is done |
[in] | expand | the dirt mask before replacing the pixels |
[in] | win_size | size of adaptive mean filtering window |
[in] | smooth | triangular filter whole image for grain removal |
[in] | inner | find crop within or outside the image's edges |
[out] | crop | array of 4 integers, if non-NULL, top, bottom, left and right values for cropping are returned. |
The main purpose of this routine is to replace dirty pixels. As spin-off it obtains half of what is needed for film grain smoothening and most of how to crop positive film. To speed things up these functions are also implemented.
Definition at line 1296 of file sanei_ir.c.
SANE_Status sanei_ir_filter_madmean | ( | const SANE_Parameters * | params, |
const SANEI_IR_bufptr | in_img, | ||
SANE_Byte ** | out_img, | ||
int | win_size, | ||
int | a_val, | ||
int | b_val | ||
) |
Find noise by adaptive thresholding.
[in] | params | pointer to image description |
[in] | in_img | pointer to grey scale image |
[out] | out_img | address of pointer to newly allocated binary image |
[in] | win_size | Size of filtering window |
[in] | a_val | Parameter, below is definetly clean |
[in] | b_val | Parameter, above is definetly noisy |
This routine follows the concept of Crnojevic's MAD (median of the absolute deviations from the median) filter. The first median filter step is replaced with a mean filter. The dirty pixels which we wish to remove are always darker than the real signal. But at high resolutions the scanner may generate some noise and the ired cleaning step can reverse things. So a maximum filter will not do. The second median is replaced by a mean filter to reduce computation time. Inspite of these changes Crnojevic's recommendations for the choice of the parameters "a" and "b" are still valid when scaled to the color depth.
Crnojevic recommends 10 < a_val < 30 and 50 < b_val < 100 for 8 bit color depth
Definition at line 915 of file sanei_ir.c.
SANE_Status sanei_ir_filter_mean | ( | const SANE_Parameters * | params, |
const SANEI_IR_bufptr | in_img, | ||
SANEI_IR_bufptr | out_img, | ||
int | win_rows, | ||
int | win_cols | ||
) |
Optimized mean filter.
[in] | params | pointer to image description |
[in] | in_img | Pointer to grey scale image data |
[out] | out_img | Pointer to grey scale image data |
[in] | win_rows | Height of filtering window, odd |
[in] | win_cols | Width of filtering window, odd |
Definition at line 731 of file sanei_ir.c.
void sanei_ir_find_crop | ( | const SANE_Parameters * | params, |
unsigned int * | dist_map, | ||
int | inner, | ||
int * | edges | ||
) |
Suggest cropping for dark margins of positive film.
[in] | params | pointer to image description |
[in] | dist_map | integer pointer to map of closest distances |
[in] | inner | crop within (!=0) or outside (==0) the image's edges |
[out] | edges | pointer to array holding top, bottom, left and right edges |
The distance map as calculated by sane_ir_manhattan_dist contains distances to the next clean pixel. Dark margins are detected as dirt. So the first/last rows/columns tell us how to crop. This is rather fast if the distance map has been calculated anyhow.
Definition at line 1213 of file sanei_ir.c.
void sanei_ir_init | ( | void | ) |
Initialize sanei_ir.
Call this before any other sanei_ir function.
Definition at line 52 of file sanei_ir.c.
SANE_Status sanei_ir_threshold_maxentropy | ( | const SANE_Parameters * | params, |
double * | norm_histo, | ||
int * | thresh | ||
) |
Implements a Maximum Entropy thresholding method.
[in] | params | describes image |
[in] | norm_histo | points to a normalized histogram |
[out] | thresh | found threshold |
Definition at line 332 of file sanei_ir.c.
SANE_Status sanei_ir_threshold_otsu | ( | const SANE_Parameters * | params, |
double * | norm_histo, | ||
int * | thresh | ||
) |
Implements Otsu's thresholding method.
[in] | params | describes image |
[in] | norm_histo | points to a normalized histogram |
[out] | thresh | found threshold |
Definition at line 249 of file sanei_ir.c.
SANE_Status sanei_ir_threshold_yen | ( | const SANE_Parameters * | params, |
double * | norm_histo, | ||
int * | thresh | ||
) |
Implements Yen's thresholding method.
[in] | params | describes image |
[in] | norm_histo | points to a normalized histogram |
[out] | thresh | found threshold |
Definition at line 173 of file sanei_ir.c.