0

I am trying to register two 16bit images. One is a .dcm CT series and the other is TIFF image. Both are uint16 type and while running SIFT I get this error:

cv2.error: OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\features2d\src\sift.dispatch.cpp:512: error: (-5:Bad argument) image is empty or has incorrect depth (!=CV_8U) in function 'cv::SIFT_Impl::detectAndCompute'

It appears OpenCV only works with 8bit (atleast that's the way I understand the message), so my question is...does anybody have any work-arounds?

I could transfer the images to uint8, but it messes up the CT series and overall decreases accuracy of the measurement which is suboptimal.

I haven't tried anything as I haven't figured out anything.

Here is the code:

def __init__(self, film_path, CT_path, min_match_count=10):
    self.detector = cv.SIFT_create()
    self.img_g = cv.cvtColor(tiff.imread(film_path), cv.COLOR_BGR2GRAY)
    if '.dcm' in CT_path:
        self.CT = dcm.dcmread(CT_path).pixel_array
    else:
        self.CT_dir = os.listdir(CT_path)
    self.min_match_count = min_match_count

def load_ct(self):
    for slc in self.CT_dir:
        CT_slice = dcm.dcmread(CT_path).pixel_array
            CT.append(CT_slice)
    

    return np.array(CT)

def detect(self, CT2detect):
    kp_self, des_self = self.detector.detectAndCompute(self.img_g, None)

    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    matcher = cv.FlannBasedMatcher(index_params, search_params)

    img = CT2detect[:, :, 383]
    im = img.copy()
    kp, des = self.detector.detectAndCompute(img, None)
    ...
Tellun
  • 1
  • 1
  • Please edit your question to show the code that produced the error. – Woodford Aug 15 '23 at 16:53
  • Error message says the file is either empty or not uint8. So it looks like you need to convert to uint8. – fmw42 Aug 15 '23 at 17:34
  • Thats the problem...I dont want to, because it messes up the CT and decreases accuracy of the measurement. – Tellun Aug 15 '23 at 17:46
  • Can you decrease the bit-depth to 8 and get the transformation matrix but then apply the result to your 16-bit image to retain the accuracy? – Mark Setchell Aug 15 '23 at 17:57
  • @MarkSetchell That's a great idea...I also have a problem with the CT tho(honestly don't know why, but it get distorted when I decrease it to 8-bit)...will try it anyway – Tellun Aug 15 '23 at 18:07
  • Depending upon how you reduce to 8-bits (you may get clipping), your transformation may get messed up due to the clipping. You need to scale the data and not just drop bits. – fmw42 Aug 15 '23 at 23:02
  • Can you show how you tried to convert to 8 bit? – Micka Aug 16 '23 at 13:40

0 Answers0