Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7745709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:05:25+00:00 2026-06-01T10:05:25+00:00

I am writing a small program in C++ using OpenCV-2.3 API. I have an

  • 0

I am writing a small program in C++ using OpenCV-2.3 API.
I have an issue processing an adaptive threshold using a non rectangular mask.

So far, I was performing the adaptive threshold on the whole image and masking afterwards. I realise that,in my case , this was a mistake since the masked pixels would be used to calculate the threshold of my pixels of interest (while I simply want to exclude the former from the analysis)…
However, unlike functions such as cv:: norm, cv::adaptiveThreshold does not seem to support explicitly a mask.

Do you know any obvious solution or workaround?
Thank you very muck for your suggestions,
Quentin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:05:26+00:00Added an answer on June 1, 2026 at 10:05 am

    I’ve written some Python (sorry not c++) code that will allow for masked adaptive thresholding. Its not very fast, but it does what you want, and you may be able to use it as a basis for C++ code. It works as follows:

    1. Sets masked pixels in the image to zero.
    2. Determines the number of unmasked neighbours within the convolution block for each pixel.
    3. Performs a convolution, and averages it by the number of unmasked neighbours within the block. This yields the average value within a pixels neighbourhood block.
    4. Thresholds, by comparing the image to the mean neighbourhood values, mean_conv
    5. Adds the masked off (non-thresholded) part of the image back on.

    enter image description here

    The images show, the initial image, the mask, the final processed image.

    Here’s the code:

    import cv
    import numpy
    from scipy import signal
    
    def thresh(a, b, max_value, C):
        return max_value if a > b - C else 0
    
    def mask(a,b):
        return a if b > 100 else 0
    
    def unmask(a,b,c):
        return b if c > 100 else a
    
    v_unmask = numpy.vectorize(unmask)
    v_mask = numpy.vectorize(mask)
    v_thresh = numpy.vectorize(thresh)
    
    def block_size(size):
        block = numpy.ones((size, size), dtype='d')
        block[(size - 1 ) / 2, (size - 1 ) / 2] = 0
        return block
    
    def get_number_neighbours(mask,block):
        '''returns number of unmasked neighbours of every element within block'''
        mask = mask / 255.0
        return signal.convolve2d(mask, block, mode='same', boundary='symm')
    
    def masked_adaptive_threshold(image,mask,max_value,size,C):
        '''thresholds only using the unmasked elements'''
        block = block_size(size)
        conv = signal.convolve2d(image, block, mode='same', boundary='symm')
        mean_conv = conv / get_number_neighbours(mask,block)
        return v_thresh(image, mean_conv, max_value,C)
    
    image = cv.LoadImageM("image.png", cv.CV_LOAD_IMAGE_GRAYSCALE)
    mask = cv.LoadImageM("mask.png", cv.CV_LOAD_IMAGE_GRAYSCALE)
    
    #change the images to numpy arrays
    original_image = numpy.asarray(image)
    mask = numpy.asarray(mask)
    # Masks the image, by removing all masked pixels.
    # Elements for mask > 100, will be processed
    image = v_mask(original_image, mask)
    # convolution parameters, size and C are crucial. See discussion in link below.
    image = masked_adaptive_threshold(image,mask,max_value=255,size=7,C=5)
    # puts the original masked off region of the image back
    image = v_unmask(original_image, image, mask)
    #change to suitable type for opencv
    image = image.astype(numpy.uint8)
    #convert back to cvmat
    image = cv.fromarray(image)
    
    cv.ShowImage('image', image)
    #cv.SaveImage('final.png',image)
    cv.WaitKey(0)
    

    After writing this I found this great link that has a good explanation with plenty of image examples, I used their text image for the above example.

    Note. Numpy masks do not seem to be respected by scipy signal.convolve2d(), so the above workarounds were necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small Heads or Tails program using the Random function and
I am writing a small image/video upload program using WCF REST. Ideally, I would
I'm writing a program using Java non-blocking socket and TCP. I understand that TCP
I'm writing a small program (using Cocoa Touch), which communicates with a webservice. The
I'm writing small application using OpenCV. Everything works great on my computer. I can
I'm writing a small program using CLR console application and when I write something
I'm writing a small program to record reading progress, the data models are simple:
I am writing a small program for our local high school (pro bono). The
I am writing a small program that gets activated when sms is received.We need
Today I am writing a small program in Haskell. I found that in ghci's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.