admin管理员组

文章数量:1406926

Photoshop has a lot of cool artistic filters, and I'd love to understand the underlying algorithms.
One algorithm that's particularly interesting is Crosshatch filter.
Is there any source for technical descriptions of the Photoshop filters? Alternatively, do you have any thoughts about how this particular filter might be implemented?

Photoshop has a lot of cool artistic filters, and I'd love to understand the underlying algorithms.
One algorithm that's particularly interesting is Crosshatch filter.
Is there any source for technical descriptions of the Photoshop filters? Alternatively, do you have any thoughts about how this particular filter might be implemented?

Share edited Mar 4 at 8:09 asele asked Mar 4 at 8:03 aseleasele 996 bronze badges 4
  • en.wikipedia./wiki/Ordered_dithering - But if you want Photoshop's implementation, you need to ask them. – Ted Lyngmo Commented Mar 4 at 8:48
  • 2 You can achieve this type of effect with ImageMagick and Fred Weinhaus's bash script fmwconcepts/imagemagick/crosshatch/index.php You could amend the script to preserve the intermediate images in order to understand how it works. Note that you would likely need to pay a licence fee for commercial use. – Mark Setchell Commented Mar 4 at 10:12
  • 2 @MarkSetchell very thanks for your advice – asele Commented Mar 4 at 10:14
  • 1 I get something pretty similar with ./crosshatch -l 5 -s 3 -g 1 -p 5 -e normal bird.png result.png – Mark Setchell Commented Mar 4 at 11:43
Add a comment  | 

1 Answer 1

Reset to default 3

You can do something like a Photoshop crosshatch with a 2D convolution with OpenCV in Python:

#!/usr/bin/env python3

import cv2 as cv
import numpy as np

# Load image
im = cv.imread('bird.png')

# Define +/- 45 degree kernel, change length to taste
len=11
kern = np.eye(len, dtype=np.float32)
kern = np.maximum(kern, kern[:,::-1]) / ( 2 * len)
print(kern)

# Apply 2D convolution to image
res = cv.filter2D(im, -1, kern)

# Save result
cv.imwrite('result.png', res)


If I change the length of the convolution kernel to 6, you can see its shape and content better. It is basically the sum of two diagonal matrices:

[[0.08333334 0.         0.         0.         0.         0.08333334]
 [0.         0.08333334 0.         0.         0.08333334 0.        ]
 [0.         0.         0.08333334 0.08333334 0.         0.        ]
 [0.         0.         0.08333334 0.08333334 0.         0.        ]
 [0.         0.08333334 0.         0.         0.08333334 0.        ]
 [0.08333334 0.         0.         0.         0.         0.08333334]]

If, instead of opening your image, I just create a 400x400 black image and place a single white dot at the centre, then do the convolution, you will see what it does:

#!/usr/bin/env python3

import cv2 as cv
import numpy as np

# Create square black image with a single white pixel in the middle
im = np.zeros((400,400), dtype=np.uint8)
im[200,200] = 255

# Define +/- 45 degree kernel, change length to taste
len=24
kern = np.eye(len, dtype=np.float32)
kern = np.maximum(kern, kern[:,::-1]) / ( 2 * len)
print(kern)

# Apply 2D convolution to image
res = cv.filter2D(im, -1, kern)

# Save result
cv.imwrite('result.png', res)

本文标签: algorithmHow is the photoshop Crosshatch filter implementedStack Overflow