0

(I am new to this whole open cv thing) My cropping method is to click a point on the image near the top left where you want the first box and the clicked point will be the starting coordinate for the rest of the cropping. (if there is an easier method you know of, i'd be grateful for ideas)

Problem: my output images are not same in clarity. I checked this using image J and the brightness/contrast analyze function. I tried to find where the reduction is coming from in the code and I think it is from imwrite but I'm not sure; I assume imwrite is messing with the metadata. I also tried using tifffile library but it did not help.

import cv2
import tifffile as tiff

image = cv2.imread('vv.tif', cv2.IMREAD_UNCHANGED)
image_copy = image.copy()

cv2.namedWindow('image')

def crop_images(x, y):
    for i in range(4):
        for j in range(14):
            cropped_image = image[y-260+i*900:y+260+i*900, x-260+j*900:x+260+j*900]

            tiff.imwrite('cropped_image_{0}.tif'.format(i*14+j), cropped_image)

def get_coordinates(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONUP:
        crop_images(x, y)
        cv2.imshow('image', image_copy)

cv2.setMouseCallback('image', get_coordinates)

cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

What I want is for the 56 cropped images to have the same brightness/contrast and clarity as original. The original image is over 2Mib so this is a screenshot of it just for reference: screenshot

  • What image format is the original? Does it have a color profile? If so, OpenCV, ignores it, I believe. That could be your issue. If it is a DNG file, then the DNG has brightness corrections that again would be ignored. – fmw42 Apr 30 '23 at 23:43
  • @fmw42 the format is tiff and it does not have a color profile. do you have any other ideas/functions/libraries i could try – sara kim May 01 '23 at 01:02
  • Post your original tiff image to some free hosting service (zip it first) and then put the URL here so I can examine the file – fmw42 May 01 '23 at 03:52
  • @fmw42 i used google drive: here is the image file zip https://drive.google.com/file/d/1F54jWchxZuGEEMTCI1xbV1KxKE87Vr-6/view?usp=sharing – sara kim May 01 '23 at 05:38
  • Regarding the **display** of 16bit images, there is no unique way of displaying them. There is no display-hardware that can show 16bit images. Consequently, only a certain range of gray-values can be displayed on available hardware. How this is done, depends on the software. Please note that this concerns the image display only, **not** the image data! – Herbie May 01 '23 at 11:51
  • I ran your script and see no differences between your saved to disk files and the original. – fmw42 May 01 '23 at 16:01

1 Answers1

0

An ImageJ-macro that does what you want:

//imagej-macro "saveTiles.ijm" (Herbie G., 01. May 2023)
run("Select None");
run("Rotate... ", "angle=0.17 grid=1 interpolation=Bilinear");
setTool("point");
waitForUser("Set point-selection to top-left corner of first tile.");
sz=624;
inc=900;
img=File.nameWithoutExtension;
pth=getDir("image")+img+"_CroppedImages";
File.makeDirectory(pth);
setBatchMode(true);
getSelectionBounds(x,y,na,na);
makeRectangle(x,y,sz,sz);
n=14;
for (j=0;j<4;j++) { 
   k=j*inc;
   for (i=0;i<n;i++) {
      setSelectionLocation(x+i*inc,y+k);
      run("Duplicate...","title=cropped");
      saveAs("tiff",pth+File.separator+img+"-"+String.pad(j*n+i+1,2));
      close();
   }
}
run("Select None");
setBatchMode(false);
exit();
//imagej-macro "saveTiles.ijm" (Herbie G., 01. May 2023)

Try first with the provided sample image! Open the sample image in ImageJ and paste the above macro code to an empty macro window (Plugins >> New >> Macro). Then run it.

Please note that the sample image is rotated by about 0.17deg to the left. The above macro first re-rotates the sample image. However, the rotation may be different for other images!

The cropped images are saved to a new directory that is located in the directory of the original image.

Herbie
  • 143
  • 5