Replies: 4 comments
-
This is a continuation of #7850. I gather 'abw' stands for 'adaptive black white'. Does it seem a bit specific to add this to ImageMath, considering that there isn't support for simple thresholding either? Would you mind sharing the addition that you made locally - possibly as the basis for the future function, but also because I'm wondering if it was fairly simple for you to add it locally, and if the more generic solution is just to document an example of such an addition? |
Beta Was this translation helpful? Give feedback.
-
Simple thresholding is typically (and very performantly) handled by Without an I will add the code changes here. I believe I had to touch two files. |
Beta Was this translation helpful? Give feedback.
-
def imagemath_abw(self: _Operand, other: _Operand, ioffset: int, ithresh: int) -> _Operand:
if isinstance(self, _Operand):
if self.im.mode not in ("L"): self.im.convert("L")
else:
msg = f"unsupported mode: {self.im.mode}"
raise ValueError(msg)
if isinstance(other, _Operand):
if other.im.mode not in ("L"): self.im.convert("L")
elif other is not None:
msg = f"unsupported mode: {other.im.mode}"
raise ValueError(msg)
if not(isinstance(ioffset,int)) or not(isinstance(ithresh,int)):
msg = f"last arguments non ints"
raise ValueError(msg)
self.im.load()
if other is None:
other_im = self.im.im.id
ioffset = 0
else:
other.im.load()
other_im = other.im.im.id
out = Image.new("1", self.im.size, None)
_imagingmath.abw(out.im.id, self.im.im.id,other_im,ioffset,ithresh)
return _Operand(out)
ops = {
"int": imagemath_int,
"float": imagemath_float,
"equal": imagemath_equal,
"notequal": imagemath_notequal,
"min": imagemath_min,
"max": imagemath_max,
"abw": imagemath_abw,
"convert": imagemath_convert,
}
static PyObject *
_abw(PyObject *self, PyObject *args) {
Imaging out;
Imaging im1,im2;
Py_ssize_t i0, i1, i2, i3, i4;
if (!PyArg_ParseTuple(args, "nnnnn", &i0, &i1, &i2, &i3, &i4)) {
return NULL;
}
out = (Imaging)i0;
im1 = (Imaging)i1;
im2 = (Imaging)i2;
int ioffset = (int)i3;
int ithresh = (int)i4;
int x, y;
for (y = 0; y < out->ysize; y++) {
UINT8 *p0 = (UINT8 *)out->image[y];
UINT8 *p1 = (UINT8 *)im1->image[y];
UINT8 *p2 = (UINT8 *)im2->image[y];
for (x = 0; x < out->xsize; x++) {
*p0 = ((*p1 < ithresh) || (*p1 < (*p2 - ioffset))) ? (UINT8)0 : (UINT8)255;
p0++;
p1++;
p2++;
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef _functions[] = {
{"unop", _unop, 1}, {"binop", _binop, 1}, {"abw",_abw,1}, {NULL, NULL}}; |
Beta Was this translation helpful? Give feedback.
-
This doesn't hinder your suggestion of a new operation, but you will be interested to know that |
Beta Was this translation helpful? Give feedback.
-
Adaptive thresholding for gray to black-and-white conversion is a bit slow. How about an Image.Math function to accelerate it?
replaced by:
I have made the addition in a local repository. It works well.
Beta Was this translation helpful? Give feedback.
All reactions