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 7989139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:39:53+00:00 2026-06-04T12:39:53+00:00

I am trying to filter an image with out using imfilter . I should

  • 0

I am trying to filter an image with out using imfilter. I should get the same results as imfilter but I keep getting diffrent results. Can someone tell me where I went wrong?

orignal=imread('obj6__17.png');
filter=1/9*[-1 -1 -1 ; -1 17 -1 ; -1 -1 -1];
s=size(orignal);
r=zeros(s(1));
temp = zeros(3);

for i= 2: s(1)-1
for j = 2: s(2)-1

    for n= 1: 3
        for m= 1:3
            temp(n,m)=orignal(i+2-n,j+2-m)*filter(n,m);
        end
    end
    r(i,j)=sum(single(sum(temp)));
end
end
  • 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-04T12:39:55+00:00Added an answer on June 4, 2026 at 12:39 pm

    The size of r should be the same as the original I think. And I don’t understand why you convert to single precision using single. Anyway, I think you want to do the following:

    %# Let's first create a small test image from the built-in peppers image
    original = im2double(imread('peppers.png'));
    original = original(1:5,1:8,1);
    
    filter = 1/9 * [-1 -1 -1 ; -1 17 -1 ; -1 -1 -1];
    s = size(original);
    r = zeros(s);
    
    for i = 2:s(1)-1
        for j = 2:s(2)-1
            temp = original(i-1:i+1,j-1:j+1) .* filter;
            r(i,j) = sum(temp(:));
        end
    end
    

    The result is as follows:

    r =
    
             0         0         0         0         0         0         0         0
             0    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344         0
             0    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240         0
             0    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436         0
             0         0         0         0         0         0         0         0
    

    And with imfilter, it is:

    r2 = imfilter(original, filter)
    
    r2 =
    
        0.3778    0.3325    0.3307    0.3442    0.3516    0.3312    0.3163    0.3856
        0.3298    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344    0.3386
        0.3434    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240    0.3512
        0.3272    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436    0.3643
        0.3830    0.3181    0.3329    0.3403    0.3508    0.3272    0.3412    0.4035
    

    As you see, the results are the same except the ones on the borders. There are a few strategies to compute the ones on the borders as mirroring the image to the out of the borders, keeping them the same, etc. Please read the documentation of imfilter and choose one strategy.

    Note that I didn’t flipped filter here since the filter is symmetric in both directions. And I recommend you to avoid loops! There are nested loops of depth four in your code!

    Lastly, you can use 2-D convolution to do the same as imfilter:

    r3 = conv2(original, filter, 'same');
    
    r3 =
    
        0.3778    0.3325    0.3307    0.3442    0.3516    0.3312    0.3163    0.3856
        0.3298    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344    0.3386
        0.3434    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240    0.3512
        0.3272    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436    0.3643
        0.3830    0.3181    0.3329    0.3403    0.3508    0.3272    0.3412    0.4035
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use CIColorMap filter, but getting run-time error [NSImage _feImage]: unrecognized selector sent
I'm trying to write a filtering function for an image, but I can't seem
I am trying to filter an image through a category. It works but not
im trying to create an image filter in OpenGL CE. currently I am trying
I'm trying to filter some XML in JavaScript using E4X and have some specific
I'm trying to filter by a list of values using the criteria API. I
I'm trying to filter webserver log files using grep. I need to output all
I am trying to filter a list so it results in a list with
I'm trying to filter through some images. Each image has 3 sets of values
Today I have been trying to get an image to save into a database,

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.