0

How could I make this heatmap look more clear without the blurred effect. How could I sharpen it Python (or ImageMagick)? also, how could I get a pixel-grid on the background?

enter image description here

Here is what I used to get the current image:

maxsize = (1030, 2056)
for i in range(1,334,1):
    img = Image.open('C:Desktop/Img/image'+'_'+ str(i)+'.png')
    img = img.resize(maxsize, Image.BICUBIC)
    img.save('C:/Desktop/Res/img'+'_'+ str(i)+'.png', dpi = (7040,7040))

I also tried in ImageMagick (but that did not help much, atleast looking visually):

magick img_244.png -sharpen 0x3 out.png

Thank you very much in advance,

Code J
  • 762
  • 1
  • 7
  • 26

2 Answers2

1

In ImageMagick (and also OpenCV/python) you can use kmeans to process your image. I have a bash unix shell script for ImageMagick that does that.

kmeans -n 7 -m 5 g4Zwf.png result7.png

where n is the number of colors and m is the maximum number of iterations.

enter image description here

Then you can use my script, grid, to draw lines (or use ImageMagick directly) to draw lines. Using my script, grid with 100 pixel spacing gives:

grid -s 100,100 -c white result7.png result7g100.png

enter image description here

My scripts are at http://www.fmwconcepts.com/imagemagick/index.html

For OpenCV/Python, see http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_kmeans/py_kmeans_opencv/py_kmeans_opencv.html and How to write lines and grid on image in Python?

OpenCV/Python also has expectation maximization that may perform better on your color processing than kmeans. See https://en.wikipedia.org/wiki/Expectation–maximization_algorithm and Maximum likelihood pixel classification in python opencv

fmw42
  • 28,780
  • 5
  • 37
  • 49
0

What module did you import as is Image? What you need is a nearest neighbor interpolation, not bicubic interpolation. Probably something like

img = img.resize(maxsize, Image.INTER_NEAREST)
LUB
  • 121
  • 4
  • I used PIL (import Image). I think the nearest neighbour interpolation is too big an interpolation and the size of the pixel (if i can say) is too huge. As a result, the level of smoothness that we want to see is not obtained. Would there be an alternative? I used this site for the reference: https://www.daniweb.com/programming/software-development/code/216637/resize-an-image-python – Code J Jul 07 '17 at 14:59
  • Your original image doesn't contain more information than you see in the nearest neighbor "interpolation". I fear I cannot help you more than just referring to the different [interpolation methods in PIL](http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize). Maybe one of them leads to something close to your desired appearance. And you could change the colormap, see [here](https://stackoverflow.com/questions/10965417/how-to-convert-numpy-array-to-pil-image-applying-matplotlib-colormap) for how to use matplotlib colormaps with PIL. – LUB Jul 07 '17 at 15:17
  • I would agree with LUB. If you have a very small original set of data and are trying to expand, then by using -resize, you are blurring it due to the pixel interpolation. All I did above was to try to separate it into unique colors. If you want to enlarge, you should either use -scale in ImageMagick or try -filter carton -resize, which might give you a bit sharper result depending upon how much enlargement you have. But LUB is correct. You do not get more information than from pixel replication using -scale. – fmw42 Jul 09 '17 at 19:36