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

The Archive Base Latest Questions

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

I have an operation that will be performed MANY times and so I need

  • 0

I have an operation that will be performed MANY times and so I need it to be as fast as possible, thus I think keeping validations to a minimum is important here.

I’m given an image, an offset and a size; and the idea is to take the color that appears the most within the area determined by the size and offset, so far I have this:

Private Shared Function someFunction(image As Bitmap, offset As Point, sampleSize As Size) As Color
    Dim pixelsColors As New List(Of Color)

    For i As Integer = offset.X To offset.X + sampleSize.Width
        For j As Integer = offset.Y To offset.Y + sampleSize.Height
            pixelsColors.Add(image.GetPixel(i, j))
        Next
    Next
End Function

What I now need is to know what is the most repeating item within the list and choose a in the case more than one have the top count, choose a random item of those that won.

I don’t even mind getting rid of the List(Of Color) and using some other (more appropriate) object.

UPDATE AFTER ACCEPTING ANSWER

The idea of the answer seems good but since there are a few minor error I’ll post this code for future reference for other.

Private Shared Function someFunction(image As Bitmap, offset As Point, sampleSize As Size) As Color
    Dim pixelsColors As New Collections.Generic.Dictionary(Of Color, Integer)

    For i As Integer = offset.X To offset.X + sampleSize.Width
        For j As Integer = offset.Y To offset.Y + sampleSize.Height
            Dim color As Color = image.GetPixel(i, j)
            Dim count As Integer
            If Not pixelsColors.TryGetValue(color, count) Then count = 0
            pixelsColors(color) = count + 1
        Next
    Next
    Return pixelsColors.OrderByDescending(Function(colorCount) colorCount.Value).First.Key
End Function
  • 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-01T00:11:06+00:00Added an answer on June 1, 2026 at 12:11 am

    Instead of storing a linear List(Of Color) you should be using a hash table which allows you to associate a Color with a count. Then you con do a simple traversal at the end to discover what the highest count is

    Dim map As New Dictionary(Of Color, Integer)
    
    For i As Integer = offset.X To offset.X + sampleSize.Width
      For j As Integer = offset.Y To offset.Y + sampleSize.Height
        Dim color = image.GetPixel(i, j)
        Dim count As Integer
        If Not map.TryGetValue(color, count) Then
          count = 0
        End If
        map(color) = count + 1
      Next
    Next
    
    Dim mostColor = map.Max(Function(x) x.Count).Key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a division operation inside a cycle that repeats many times. It so
I have a long-running cleanup operation that I need to perform in onDestroy() of
I have one costly function that gets called many times and there is a
I have a WCF service that will serve multiple clients. They will have operation
I have a function a time consuming operation that is done I want to
In My application have time consuming process.There fore i try to do that operation
I have a homework that include a new operation (a [n] b), given: a
I have a class that provide the CRUD operation for an entity.Im using the
I have a javascript function that accepts a number and performs a mathematical operation
Let's say you have a business logic method that can perform some operation across

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.