Solved

HTML5 - Canvas - how to find a shape in a canvas and count the number of red pixels

Posted on 2014-10-20
5
154 Views
Last Modified: 2014-10-28
Hi..
Any idea how I can find a shape in a canvas (image) and count the number of pixels of the shape.
Say I have an image with a circle and an a square.
How can I find / or count the number of red pixels the square takes up.
thx in advance
square.png
0
Comment
Question by:JElster
5 Comments
 
LVL 14

Assisted Solution

by:Don Thomson
Don Thomson earned 167 total points
ID: 40391987
If you can determine the dimensions of the square then calculate the number of dquare inches - and from your screen resolution - you should be able to calculate the  number of pixels.  This assumes that all the pixels are red.
0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 167 total points
ID: 40392660
If it is your own page, then you put the square in the canvas with scripting and the Object for the square should have height and wisth properties (which may be dynamic) so if you multiple the offsetWidth by the offsetHeight that should give you the total number of pixels based on the current state of the object being rendered as a square.  If you want to know how of a specific colour you would have to script a scan of the rendered area of the object and count the occurrence of the specific value.

If this is from a page you don't control then you might be able to get an approximation by diong a screen shot and counting the pixels in a graphics program.

Cd&
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40394440
You might also want to consider the definition of "red" since it might mean strictly RGB = 255,0,0 but a visual red could be many variants of the RGB values and still look quite red to the observer.  For example, in the attached image file, the red area contains RGB = 237,28,36.
0
 
LVL 1

Accepted Solution

by:
JElster earned 0 total points
ID: 40400099
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40408040
best
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trouble inserting a variable  in javascript 7 42
how would you interpret lines 3 30
Put shading on half of picture 8 28
How to get this page layout correct 13 8
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now